国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android自定義SurfaceView實現畫板功能

2019-12-12 05:51:30
字體:
來源:轉載
供稿:網友

接觸了這么久的View,總不能一直停留在View里,現在開始呢,就要學習一個新的知識點:SurfaceView,實際上SurfaceView與View的原理都差不多,只是效率和渲染方式上,SurfaceView要優于View,這也是我們寫這個的原因。今天就看看這個SurfaceView,好了,下面就是今天要說的效果。

界面很簡單,就是一個按鈕以及一個畫板,先看看界面的代碼吧

<LinearLayout 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"  android:orientation="vertical" tools:context="com.example.xinxindemo.MainActivity" > <com.example.xinxindemo.view.SecondSurfaceView  android:id="@+id/surfaceview"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:layout_weight="20"   /> <LinearLayout  android:layout_width="match_parent"  android:layout_height="55dip"  android:orientation="horizontal"  android:padding="5dip" >  <Button   android:onClick="onClick"   android:layout_width="wrap_content"   android:layout_height="match_parent"   android:text="clean" /></LinearLayout></LinearLayout>

對吧,界面不是很復雜,下面再看看這個SecondSurfaceView是怎么實現的;

/** * 2016年7月26日17:20:13 * @author 小瓶蓋 blog  * */public class SecondSurfaceView extends SurfaceView implements SurfaceHolder.Callback,Runnable{ /**  * 是否處于繪制狀態  */ private boolean mIsDrawing; /**  * 幫助類  */ private SurfaceHolder mHolder; /**  * 畫布  */ private Canvas mCanvas; /**  * 路徑  */ private Path mPath; /**  * 畫筆  */ private Paint mPaint; public SecondSurfaceView(Context context, AttributeSet attrs, int defStyle) {  super(context, attrs, defStyle);  initView(); } public SecondSurfaceView(Context context, AttributeSet attrs) {  super(context, attrs);  initView(); } public SecondSurfaceView(Context context) {  super(context);  initView(); } @Override public boolean onTouchEvent(MotionEvent event) {  int x=(int) event.getX();  int y=(int) event.getY();  switch (event.getAction()) {  case MotionEvent.ACTION_DOWN:   mPath.moveTo(x, y);   break;  case MotionEvent.ACTION_MOVE:   mPath.lineTo(x, y);   break;  case MotionEvent.ACTION_UP:   break;  default:   break;  }  return true; } private void initView() {  mHolder=getHolder();  mHolder.addCallback(this);  setFocusable(true);  setFocusableInTouchMode(true);  this.setKeepScreenOn(true);  mPath=new Path();  mPaint=new Paint();  mPaint.setAntiAlias(true);  mPaint.setColor(Color.BLACK);  mPaint.setStyle(Style.STROKE);  mPaint.setStrokeWidth(15); } @Override public void run() {  long start =System.currentTimeMillis();  while(mIsDrawing){   draw();  }  long end =System.currentTimeMillis();  if (end-start<100) {   try {    Thread.sleep(100-(end-start));   } catch (Exception e) {    e.printStackTrace();   }  } } @Override public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { } @Override public void surfaceCreated(SurfaceHolder arg0) {  mIsDrawing=true;  new Thread(this).start(); } @Override public void surfaceDestroyed(SurfaceHolder arg0) {  mIsDrawing=false; } private void draw(){  try {   mCanvas=mHolder.lockCanvas();   mCanvas.drawColor(Color.WHITE);   mCanvas.drawPath(mPath, mPaint);  } catch (Exception e) {   e.printStackTrace();  }finally{   if (mCanvas!=null) {    mHolder.unlockCanvasAndPost(mCanvas);   }  } } /**  * 清除內容  */ public void clean(){  initView(); }}

然后就是MainActivity.java

/** * 2016年7月26日17:20:13 * @author 小瓶蓋 blog * */public class MainActivity extends Activity{ SecondSurfaceView surfaceView; @Override protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main);  surfaceView=(SecondSurfaceView)findViewById(R.id.surfaceview); } public void onClick(View v){  surfaceView.clean(); }}

源碼下載:http://xiazai.VeVB.COm/201607/yuanma/SurfaceView(VeVB.COm).rar

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 崇仁县| 崇州市| 鸡泽县| 北票市| 手游| 项城市| 托克托县| 汶川县| 东台市| 临澧县| 衡阳市| 盈江县| 芮城县| 霞浦县| 博爱县| 美姑县| 福贡县| 巴中市| 旬阳县| 当雄县| 曲周县| 镇安县| 安阳市| 望都县| 吉木萨尔县| 万全县| 紫金县| 临漳县| 福贡县| 临夏县| 缙云县| 天柱县| 广西| 普兰店市| 通海县| 天等县| 唐海县| 建瓯市| 灵丘县| 远安县| 鄂托克前旗|