扣丁学堂——SurfaceView(实现画图 播放视频)
2016-01-18 15:23
330 查看
一丶代码见本文
二丶课程讲解
MainActivity
MainActivity
xml
二丶课程讲解
/** * 自定义 surfaceView(相当于独立的一块屏幕) */ public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback { //控制屏幕绘制的对象 private SurfaceHolder holder; private MyThread thread; public MySurfaceView(Context context) { super(context); holder = this.getHolder(); //添加回调接口,因为实现了,所以传this,对象一创建就调用回调的方法 holder.addCallback(this); } class MyThread implements Runnable { private SurfaceHolder holder; //作用:线程什么进侯开始,什么时侯结束 public boolean isRun; public MyThread(SurfaceHolder holder) { this.holder = holder; isRun = true; } @Override public void run() { int count = 0; Canvas canvas = null; while (isRun) { try { synchronized (holder) { //因为有多个 线程在操作,所以要同步 //锁定一个画布,锁定后,可以通过其返回的 画布对象 Canvas,在上面画图等操作了 canvas = holder.lockCanvas(); canvas.drawColor(Color.BLACK); Paint p = new Paint(); p.setColor(Color.RED); p.setStyle(Paint.Style.FILL); p.setAntiAlias(true); canvas.drawRect(10, 10, 100, 100, p); p.setTextSize(30); canvas.drawText("当前是第 " + (count++)+" 秒", 10, 150, p); Thread.sleep(1000); } } catch (InterruptedException e) { e.printStackTrace(); }finally { holder.unlockCanvasAndPost(canvas);//释放 画布的锁 } } } } /*************** 事件回调方法 *********************************/ /** * 回调 * * @param holder */ @Override public void surfaceCreated(SurfaceHolder holder) { thread = new MyThread(holder); thread.isRun = true; new Thread(thread).start(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceDestroyed(SurfaceHolder holder) { thread.isRun = false; } }
MainActivity
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(new MyView(this)); // setContentView(new MyImageView(this)); setContentView(new MySurfaceView(this)); } }
MainActivity
public class VideoActivity extends AppCompatActivity implements SurfaceHolder.Callback{ private SurfaceView surfaceView; private SurfaceHolder holder; private MediaPlayer mp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_video); surfaceView = (SurfaceView) findViewById(R.id.surfaceView); holder = surfaceView.getHolder(); holder.addCallback(this); holder.setFixedSize(320,220); } public void playClick(View view) { mp.start(); } public void pauseClick(View view) { mp.pause(); } public void stopClick(View view) { mp.stop(); } @Override public void surfaceCreated(SurfaceHolder holder) { mp = new MediaPlayer(); mp.setAudioStreamType(AudioManager.STREAM_MUSIC); mp.setDisplay(holder); String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES)+"/2.mp4"; try { mp.setDataSource(path);//设置播放视频源 mp.prepare(); } catch (IOException e) { e.printStackTrace(); } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceDestroyed(SurfaceHolder holder) { if (mp!=null){ if (mp.isPlaying()){ mp.stop(); mp.release(); } } } @Override protected void onDestroy() { super.onDestroy(); if (mp!=null){ if (mp.isPlaying()){ mp.stop(); mp.release(); } } } }
xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.codingke.vince59_canvas_drawable.VideoActivity"> <SurfaceView android:layout_width="match_parent" android:layout_height="300dp" android:id="@+id/surfaceView" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="播放" android:onClick="playClick" android:id="@+id/button" android:layout_alignTop="@+id/button3" android:layout_toLeftOf="@+id/button2" android:layout_toStartOf="@+id/button2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="暂停" android:onClick="pauseClick" android:id="@+id/button2" android:layout_alignTop="@+id/button" android:layout_centerHorizontal="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="停止" android:onClick="stopClick" android:id="@+id/button3" android:layout_below="@+id/surfaceView" android:layout_toRightOf="@+id/button2" android:layout_toEndOf="@+id/button2" /> </RelativeLayout>
相关文章推荐
- Picasso加载图片添加请求头addHeader
- 数据库---MySQL练习题及答案
- UItableView搜索和全部删除
- 操作系统与网络实现 之十四
- 【设计模式】--模板方法
- Truncate
- struts返回json数据解析
- VS2010 使用TeeChart绘图控件 - 之二 - 绘制图形(折线图,柱状图)
- webservice cxf
- 回调函数API
- oracle中trim,ltrim,rtrim函数用法
- 数据库 创建 查询 练习
- 图片左右滚动的js代码
- webkit内核(chrome、safari) 、FF、IE 、opera 下的linear-gradient 写法
- 蓝桥杯 历届试题 剪格子
- 编写python脚本-->批量下载淘宝 2012年ADC技术嘉年华PDF文件
- obj-c内存管理的规则
- UICollectionView的基本使用
- Android 着色器 Tint 研究
- .net中对象序列化技术浅谈