close
package com.example.videoview; //package com.example.framelayout_2_videoview; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.view.Menu; /////////////////////////////////////// import android.view.View; import android.util.DisplayMetrics; import android.view.Window; import android.view.WindowManager; import android.widget.Button; import android.view.*; import android.view.View.*; import android.widget.*; public class MainActivity extends Activity { Button bt; FrameLayout FrameLayout2; FrameLayout FrameLayout1; DisplayMetrics metrics; VideoView VideoView1; int width,height; int hit=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ///////////////////////////////////////// requestWindowFeature(Window.FEATURE_NO_TITLE); //全螢幕設定 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); metrics = new DisplayMetrics();//抓取螢幕大小 getWindowManager().getDefaultDisplay().getMetrics(metrics); ///////////////////////////////////////// FrameLayout1 = new FrameLayout(this); FrameLayout1.setLayoutParams(new FrameLayout.LayoutParams(metrics.widthPixels,metrics.heightPixels)); this.setContentView(FrameLayout1); //////////////////////////////////////////// // FrameLayout2=new FrameLayout(this); // FrameLayout.LayoutParams lparam2 = new FrameLayout.LayoutParams(metrics.widthPixels,metrics.heightPixels); // lparam2.gravity = Gravity.LEFT|Gravity.TOP; // lparam2.leftMargin = 0;//X // lparam2.topMargin = metrics.heightPixels/2;//Y // FrameLayout2.setLayoutParams(lparam2); /////////////////////////////////// VideoView1=new VideoView(this){ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { //setMeasuredDimension(width,height); //width = getDefaultSize(width, widthMeasureSpec); //height = getDefaultSize(height, heightMeasureSpec); setMeasuredDimension(width, height); } }; //////////////////////////////////// VideoView1.setVideoURI(Uri.parse("/sdcard/a.mp4")); MediaController mediaController = new MediaController(this); VideoView1.setMediaController(mediaController); //////////////////////////////////////////////////// FrameLayout.LayoutParams layoutParams_VideoView = new FrameLayout.LayoutParams(metrics.widthPixels, metrics.heightPixels); layoutParams_VideoView.gravity = Gravity.LEFT|Gravity.TOP; layoutParams_VideoView.leftMargin = 0;//X layoutParams_VideoView.topMargin = 0;//Y VideoView1.setLayoutParams(layoutParams_VideoView); width=metrics.widthPixels; height=metrics.heightPixels; VideoView1.getHolder().setFixedSize(width, height); VideoView1.requestLayout(); VideoView1.invalidate(); //////////////////////////////////////// // bt=new Button; bt=new Button(this); bt.setText("start!!"); //bt.setWidth(metrics.widthPixels/10); //bt.setHeight(metrics.heightPixels/9); bt.setOnClickListener(new ButListener()); FrameLayout.LayoutParams layoutParams0 = new FrameLayout.LayoutParams(metrics.widthPixels/4, metrics.heightPixels/10); layoutParams0.gravity = Gravity.LEFT|Gravity.TOP; layoutParams0.leftMargin = 0; layoutParams0.topMargin = 0; bt.setLayoutParams(layoutParams0); FrameLayout1.addView(bt); ///////////////////////////////////////////////// //FrameLayout2.addView(VideoView1); //FrameLayout2.setVisibility(View.VISIBLE);//show VideoView1.start(); ///////////////////////////////////////////////// FrameLayout1.addView(VideoView1); FrameLayout1.setVisibility(View.VISIBLE); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } class ButListener implements OnClickListener { public void onClick(View v) { if (v==bt) { if (hit==0) { bt.setText("Hit !!"); FrameLayout.LayoutParams layoutParams_VideoView = new FrameLayout.LayoutParams(metrics.widthPixels, metrics.heightPixels); layoutParams_VideoView.gravity = Gravity.LEFT|Gravity.TOP; layoutParams_VideoView.leftMargin = 0;//X layoutParams_VideoView.topMargin = 0;//Y VideoView1.setLayoutParams(layoutParams_VideoView); width=metrics.widthPixels; height=metrics.heightPixels; VideoView1.getHolder().setFixedSize(width, height); VideoView1.requestLayout(); VideoView1.invalidate(); hit++; } else if (hit==1) { bt.setText("hit !!"); FrameLayout.LayoutParams layoutParams_VideoView = new FrameLayout.LayoutParams(metrics.widthPixels/2, metrics.heightPixels/2); layoutParams_VideoView.gravity = Gravity.LEFT|Gravity.TOP; layoutParams_VideoView.leftMargin = 0;//X layoutParams_VideoView.topMargin = 0;//Y VideoView1.setLayoutParams(layoutParams_VideoView); width=metrics.widthPixels/2; height=metrics.heightPixels/2; VideoView1.getHolder().setFixedSize(width, height); VideoView1.requestLayout(); VideoView1.invalidate(); hit--; } } } } }
文章標籤
全站熱搜
留言列表