2013年12月29日 星期日

透過兩個按鈕配合FrameLayout操控VideoView大小與位置

透過兩個按鈕配合FrameLayout操控VideoView大小與位置



 


自己撰寫透過兩個FrameLayout和兩個按鈕來操控VideoView大小與位置,歡迎有興趣的同好一起來C/P一下。


 


 









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.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;
FrameLayout FrameLayout2;
FrameLayout FrameLayout1;
DisplayMetrics metrics;
VideoView VideoView1;
int width,height;
int m_intswich;
int m_intmove;
@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);
FrameLayout.LayoutParams lparam1 = new FrameLayout.LayoutParams(metrics.widthPixels,metrics.heightPixels);
FrameLayout1.setLayoutParams(lparam1);
this.setContentView(FrameLayout1);
////////////////////////////////////////
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].setOnClickListener(new ButListener());
FrameLayout.LayoutParams layoutParams0 = new FrameLayout.LayoutParams(metrics.widthPixels/2, metrics.heightPixels/2);
layoutParams0.gravity = Gravity.LEFT|Gravity.TOP;
layoutParams0.leftMargin = 0;
layoutParams0.topMargin = 0;
bt[0].setLayoutParams(layoutParams0);
FrameLayout1.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());
FrameLayout.LayoutParams layoutParams1 = new FrameLayout.LayoutParams(metrics.widthPixels/2, metrics.heightPixels/2);
layoutParams1.gravity = Gravity.LEFT|Gravity.TOP;
layoutParams1.leftMargin = metrics.widthPixels/2;
layoutParams1.topMargin = 0;
bt[1].setLayoutParams(layoutParams1);
FrameLayout1.addView(bt[1]);
////////////////////////////////////////////
FrameLayout2=new FrameLayout(this);
FrameLayout.LayoutParams lparam2 = new FrameLayout.LayoutParams(metrics.widthPixels,metrics.heightPixels/2);
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/jash.mp4"));
MediaController mediaController = new MediaController(this);
VideoView1.setMediaController(mediaController);

////////////////////////////////////////////////////
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();

/////////////////////////////////////////////////
FrameLayout2.addView(VideoView1);
FrameLayout2.setVisibility(View.VISIBLE);//show
VideoView1.start();
/////////////////////////////////////////////////
FrameLayout1.addView(FrameLayout2);

m_intswich=0;
m_intmove=0;
}

@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[1].setText("1");
if(m_intmove==0)
{
bt[0].setWidth(metrics.widthPixels/2);
bt[0].setHeight(metrics.heightPixels/2);
bt[0].setOnClickListener(new ButListener());
FrameLayout.LayoutParams layoutParams0 = new FrameLayout.LayoutParams(metrics.widthPixels/2, metrics.heightPixels/2);
layoutParams0.gravity = Gravity.LEFT|Gravity.TOP;
layoutParams0.leftMargin = 0;
layoutParams0.topMargin = 0;
bt[0].setLayoutParams(layoutParams0);

bt[1].setWidth(metrics.widthPixels/2);
bt[1].setHeight(metrics.heightPixels/2);
bt[1].setOnClickListener(new ButListener());
FrameLayout.LayoutParams layoutParams1 = new FrameLayout.LayoutParams(metrics.widthPixels/2, metrics.heightPixels/2);
layoutParams1.gravity = Gravity.LEFT|Gravity.TOP;
layoutParams1.leftMargin = metrics.widthPixels/2;
layoutParams1.topMargin = 0;
bt[1].setLayoutParams(layoutParams1);

FrameLayout.LayoutParams lparam2 = new FrameLayout.LayoutParams(metrics.widthPixels,metrics.heightPixels/2);
lparam2.gravity = Gravity.LEFT|Gravity.TOP;
lparam2.leftMargin = 0;//X
lparam2.topMargin = metrics.heightPixels/2;//Y
FrameLayout2.setLayoutParams(lparam2);

m_intmove++;
}
else
{
m_intmove=0;

bt[0].setWidth(metrics.widthPixels/2);
bt[0].setHeight(metrics.heightPixels/2);
bt[0].setOnClickListener(new ButListener());
FrameLayout.LayoutParams layoutParams0 = new FrameLayout.LayoutParams(metrics.widthPixels/2, metrics.heightPixels/2);
layoutParams0.gravity = Gravity.LEFT|Gravity.TOP;
layoutParams0.leftMargin = 0;//X
layoutParams0.topMargin = metrics.heightPixels/2;//y
bt[0].setLayoutParams(layoutParams0);

bt[1].setWidth(metrics.widthPixels/2);
bt[1].setHeight(metrics.heightPixels/2);
bt[1].setOnClickListener(new ButListener());
FrameLayout.LayoutParams layoutParams1 = new FrameLayout.LayoutParams(metrics.widthPixels/2, metrics.heightPixels/2);
layoutParams1.gravity = Gravity.LEFT|Gravity.TOP;
layoutParams1.leftMargin = metrics.widthPixels/2;//x
layoutParams1.topMargin = metrics.heightPixels/2;//y
bt[1].setLayoutParams(layoutParams1);

FrameLayout.LayoutParams lparam2 = new FrameLayout.LayoutParams(metrics.widthPixels,metrics.heightPixels/2);
lparam2.gravity = Gravity.LEFT|Gravity.TOP;
lparam2.leftMargin = 0;//X
lparam2.topMargin = 0;//Y
FrameLayout2.setLayoutParams(lparam2);
}

FrameLayout.LayoutParams layoutParams_VideoView = new FrameLayout.LayoutParams(metrics.widthPixels, 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;
height=metrics.heightPixels/2;
VideoView1.getHolder().setFixedSize(width, height);
VideoView1.requestLayout();
VideoView1.invalidate();
}
if(v==bt[1])
{
bt[1].setText("1 clicked...");
bt[0].setText("0");
FrameLayout.LayoutParams layoutParams_VideoView = new FrameLayout.LayoutParams(metrics.widthPixels/2, metrics.heightPixels/2);
layoutParams_VideoView.gravity = Gravity.LEFT|Gravity.TOP;
if(m_intswich==0)
{
layoutParams_VideoView.leftMargin = 0;//X
layoutParams_VideoView.topMargin = 0;//Y
m_intswich++;
}
else
{
m_intswich=0;
layoutParams_VideoView.leftMargin = metrics.widthPixels/2;//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();
}
}

}
}


沒有留言:

張貼留言