91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

詳解Android中SurfaceView畫板操作

發布時間:2020-07-20 17:33:25 來源:億速云 閱讀:231 作者:小豬 欄目:移動開發

這篇文章主要詳解Android中SurfaceView畫板操作,內容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。

畫板——畫路徑

package com.example.review.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * 畫板畫路徑
 */
public class HuabanView extends SurfaceView implements SurfaceHolder.Callback {

 private SurfaceHolder surfaceHolder;
 private Path path = new Path();


 public HuabanView(Context context) {
  super(context);
 }

 public HuabanView(Context context, AttributeSet attrs) {
  super(context, attrs);
  surfaceHolder = getHolder();
  surfaceHolder.addCallback(this);//獲得surfaceview的生命周期
 }

 public HuabanView(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
 }

 public HuabanView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
  super(context, attrs, defStyleAttr, defStyleRes);
 }

 @Override
 public void surfaceCreated(SurfaceHolder holder) {
  new HuabanThread().start();
 }

 @Override
 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

 }

 @Override
 public void surfaceDestroyed(SurfaceHolder holder) {

 }

 @Override
 public boolean onTouchEvent(MotionEvent event) {
  float x = event.getX();
  float y = event.getY();
  int action = event.getAction();
  if (action == MotionEvent.ACTION_DOWN) {//按下
   path.moveTo(x, y);
  } else if (action == MotionEvent.ACTION_MOVE) {//移動
   path.lineTo(x, y);
  }
  return true;
 }

 class HuabanThread extends Thread {
  @Override
  public void run() {
   super.run();
   //TODO:畫筆
   Paint paint = new Paint();
   paint.setColor(Color.BLACK);
   paint.setStrokeWidth(20);
   paint.setStyle(Paint.Style.STROKE);
   paint.setAntiAlias(true);
   //TODO:畫布
   while (true) {
    Canvas canvas = surfaceHolder.lockCanvas();
    //避免空指針
    if (canvas == null){
     return;
    }
    canvas.drawColor(Color.WHITE, PorterDuff.Mode.CLEAR);
    canvas.drawColor(Color.WHITE);
    canvas.drawPath(path,paint);
    surfaceHolder.unlockCanvasAndPost(canvas);
   }
  }
 }
 public void close(){
  path.reset();
 }
}

畫板——畫動態直線

package com.example.review.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * 畫板畫路徑
 * 畫動態直線
 */
public class LineView extends SurfaceView implements SurfaceHolder.Callback {

 private SurfaceHolder surfaceHolder;
 private Path path = new Path();
 private int x = 0;

 public LineView(Context context) {
  super(context);
 }

 public LineView(Context context, AttributeSet attrs) {
  super(context, attrs);
  surfaceHolder = getHolder();
  surfaceHolder.addCallback(this);//獲得surfaceview的生命周期
 }

 public LineView(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
 }

 public LineView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
  super(context, attrs, defStyleAttr, defStyleRes);
 }

 @Override
 public void surfaceCreated(SurfaceHolder holder) {
  new HuabanThread().start();
 }

 @Override
 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

 }

 @Override
 public void surfaceDestroyed(SurfaceHolder holder) {

 }

 class HuabanThread extends Thread {
  @Override
  public void run() {
   super.run();
   //TODO:畫筆
   Paint paint = new Paint();
   paint.setColor(Color.BLACK);
   paint.setStrokeWidth(20);
   paint.setStyle(Paint.Style.STROKE);
   paint.setAntiAlias(true);
   //TODO:畫布
   while (true) {
    Canvas canvas = surfaceHolder.lockCanvas();
    //避免空指針
    if (canvas == null){
     return;
    }
    canvas.drawColor(Color.WHITE, PorterDuff.Mode.CLEAR);
    canvas.drawColor(Color.WHITE);
    canvas.drawLine(0,100,x++,100,paint);
    surfaceHolder.unlockCanvasAndPost(canvas);
   }
  }
 }

 public void close(){
  path.reset();
 }

}

基本圖形

//圓
canvas.drawOval(50,100,150,200,paint);
//半圓
canvas.drawArc(500,500,700,700,20,180,true,paint);
//矩形
canvas.drawRect(100,300,250,400,paint);
//三角形
canvas.drawLine(100,450,0,600,paint);
canvas.drawLine(0,600,400,600,paint);
canvas.drawLine(100,450,400,600,paint);
//梯形
canvas.drawLine(100,700,200,700,paint);
canvas.drawLine(100,700,0,900,paint);
canvas.drawLine(0,900,400,900,paint);
canvas.drawLine(200,700,400,900,paint);

//文字
canvas.drawText("截圖",100,1000,paint);

看完上述內容,是不是對詳解Android中SurfaceView畫板操作有進一步的了解,如果還想學習更多內容,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

马龙县| 苏尼特左旗| 宁海县| 翁牛特旗| 仙桃市| 奉节县| 始兴县| 嘉禾县| 无为县| 尉犁县| 宁乡县| 怀来县| 灵川县| 酒泉市| 肥城市| 宁明县| 申扎县| 乌鲁木齐县| 浏阳市| 建阳市| 商丘市| 丰县| 金乡县| 共和县| 大兴区| 芮城县| 法库县| 交城县| 中西区| 穆棱市| 怀柔区| 洛隆县| 固镇县| 苏尼特右旗| 凌海市| 云林县| 新邵县| 长治县| 类乌齐县| 明光市| 陇川县|