2013年12月26日 星期四

透過兩個按鈕配合絕對佈局元件操控VideoView大小與位置

透過兩個按鈕配合絕對佈局元件操控VideoView大小與位置


 









package com.example.abs_2_videoview;

import com.example.abs_2_videoview.MainActivity.ButListener;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
///////////////////////////////////////

import android.view.View;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.TextView;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import android.view.ViewGroup.MarginLayoutParams;
import android.app.ActionBar.LayoutParams;
import android.content.Context;

public class MainActivity extends Activity {
Button[] bt;
AbsoluteLayout AbsLayout2;
AbsoluteLayout AbsLayout1;
DisplayMetrics metrics;
VideoView VideoView1;
int width,height;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/////////////////////////////////////////
requestWindowFeature(Window.FEATURE_NO_TITLE); //全螢幕設定
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
/////////////////////////////////////////
AbsLayout1=new AbsoluteLayout(this);
setContentView(AbsLayout1);
/////////////////////////////////////////
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
bt=new Button[2];

bt[0]=new Button(this);
bt[0].setText("0");
bt[0].setWidth(metrics.widthPixels/2);
bt[0].setHeight(metrics.heightPixels/2);
bt[0].setFocusable(true);
bt[0].setOnClickListener(new ButListener());


bt[0].setLayoutParams( new AbsoluteLayout.LayoutParams( metrics.widthPixels/2, metrics.heightPixels/2, 0, 0 ) );
AbsLayout1.addView(bt[0]);

bt[1]=new Button(this);
bt[1].setText("1");
bt[1].setWidth(metrics.widthPixels/2);
bt[1].setHeight(metrics.heightPixels/2);
bt[1].setOnClickListener(new ButListener());
bt[1].setLayoutParams( new AbsoluteLayout.LayoutParams( metrics.widthPixels/2, metrics.heightPixels/2, metrics.widthPixels/2, 0 ) );
AbsLayout1.addView(bt[1]);

AbsLayout2=new AbsoluteLayout(this);
AbsLayout2.setLayoutParams(new AbsoluteLayout.LayoutParams( metrics.widthPixels, metrics.heightPixels/2, 0, metrics.heightPixels/2));
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/jash.mp4"));
MediaController mediaController = new MediaController(this);
VideoView1.setMediaController(mediaController);
VideoView1.start();
VideoView1.setLayoutParams(new AbsoluteLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT,0,0));
width=metrics.widthPixels;
height=metrics.heightPixels/2;
VideoView1.getHolder().setFixedSize(width, height);
VideoView1.requestLayout();
VideoView1.invalidate();

AbsLayout2.addView(VideoView1);

AbsLayout2.setVisibility(View.VISIBLE);//show
AbsLayout1.addView(AbsLayout2);
}

@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[0])
{
bt[0].setText("0 clicked...");
bt[0].setVisibility(View.INVISIBLE);//hide
bt[1].setVisibility(View.VISIBLE);//show
AbsLayout2.setLayoutParams(new AbsoluteLayout.LayoutParams( metrics.widthPixels, metrics.heightPixels/2, 0, metrics.heightPixels/2));
//VideoView1.setLayoutParams(new AbsoluteLayout.LayoutParams(metrics.widthPixels/2, metrics.heightPixels/2,metrics.widthPixels/2,0));//移動player OK
VideoView1.setLayoutParams(new AbsoluteLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT,0,0));//配合下面程式可以正常將畫面擴展
width=metrics.widthPixels;
height=metrics.heightPixels/2;
VideoView1.getHolder().setFixedSize(width, height);
VideoView1.requestLayout();
VideoView1.invalidate();
}
if(v==bt[1])
{
bt[1].setText("1 clicked...");
bt[1].setVisibility(View.INVISIBLE);//hide
bt[0].setVisibility(View.VISIBLE);//show
AbsLayout2.setLayoutParams(new AbsoluteLayout.LayoutParams( metrics.widthPixels/2, metrics.heightPixels/2, 0, metrics.heightPixels/2));
VideoView1.setLayoutParams(new AbsoluteLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT,0,0));
width=metrics.widthPixels/2;
height=metrics.heightPixels/2;
VideoView1.getHolder().setFixedSize(width, height);
VideoView1.requestLayout();
VideoView1.invalidate();

}
}

}
}


 


沒有留言:

張貼留言