android布局控件教程:Android多媒体入门-如何绘制一张图
android布局控件教程:Android多媒体入门-如何绘制一张图本人CSDN博客地址:https://blog.csdn.net/tong5956
目录:
1、使用SurfaceView画一张图2、自定义view绘制一张图开始认真学一下Android多媒体开发,拓宽多媒体的技术面,绘制一张图在Android中有三种方法:SurfaceView、ImageView、自定义View。
1、使用SurfaceView画一张图package com.example.tongjiangsong.surfaceviewdemo;
import android.content.Context;
import android.graphics.canvas;
import android.graphics.Color;
import android.graphics.paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class CommonSurfaceView extends SurfaceView implements SurfaceHolder.Callback Runnable {
private SurfaceHolder mHolder;
private Canvas mCanvas;
private Path mPath;
private Paint mPaint;
//用于绘制的线程
private Thread mThread;
//线程状态的标记(线程的控制开关)
private boolean isRunning;
private int drawX;
private int drawY;
public CommonSurfaceView(Context context) {
this(context null);
}
public CommonSurfaceView(Context context AttributeSet attrs) {
this(context attrs 0);
}
public CommonSurfaceView(Context context AttributeSet attrs int defStyleAttr) {
super(context attrs defStyleAttr);
//初始化
mHolder = getHolder();
mHolder.addCallback(this);//管理生命周期
mPath=new Path();
//初始化画笔
mPaint=new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(6);
mPaint.setAntiAlias(true);
mPaint.setColor(Color.RED);
setFocusable(true);
setFocusableInTouchMode(true);
this.setKeepScreenOn(true);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
isRunning = true;
mThread = new Thread(this);
mThread.start();//开启线程
}
@Override
public void surfaceChanged(SurfaceHolder holder int format int width int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
isRunning = false;
}
/***
* 注意这个是在子线程中绘制的
*/
@Override
public void run() {
while (isRunning) {
drawX ;
drawY = (int) (100 * Math.sin(drawX * 2 * Math.PI / 180) 400);
mPath.lineTo(drawX drawY);
draw();
}
}
/***
* 注意这个是在子线程中绘制的,surface支持子线程更新ui,所以
*/
private void draw() {
Canvas canvas = null;
//给画布加锁,防止线程安全,防止该内存区域被其他线程公用
canvas = mHolder.lockCanvas();
if (null != canvas) {
//清屏操作或者设置背景
canvas.drawColor(Color.WHITE);
canvas.drawPath(mPath mPaint);
//提交显示视图并解锁,防止长期占用此内存
mHolder.unlockCanvasAndPost(canvas);
}
}
}
输出结果:
2、使用自定义view绘制一张图package com.example.tongjiangsong.viewdemo;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import java.util.Random;
public class BallView extends View {
private Context mContext;
private AttributeSet mAttrs;
private Paint paint; //定义画笔
public BallView(Context context) {
super(context);
this.mContext = context;
//初始化画笔
initPaint();
}
public BallView(Context context AttributeSet attrs) {
super(context attrs);
this.mContext = context;
this.mAttrs = attrs;
initPaint();
}
public BallView(Context context AttributeSet attrs int defStyleAttr) {
super(context attrs defStyleAttr);
this.mContext = context;
this.mAttrs = attrs;
initPaint();
}
private void initPaint(){
paint = new Paint();
}
//重写onDraw方法实现绘图操作
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setAntiAlias(false); //设置画笔为无锯齿
paint.setColor(Color.BLACK); //设置画笔颜色
canvas.drawColor(Color.WHITE); //白色背景
paint.setStrokeWidth((float) 3.0); //线宽
paint.setStyle(Paint.Style.STROKE); //空心效果
canvas.drawCircle(50 50 10 paint); //绘制圆形
canvas.drawCircle(100 100 20 paint); //绘制圆形
canvas.drawCircle(150 150 30 paint); //绘制圆形
canvas.drawCircle(200 200 40 paint); //绘制圆形
canvas.drawCircle(250 250 50 paint); //绘制圆形
canvas.drawCircle(300 300 60 paint); //绘制圆形
canvas.drawCircle(350 350 70 paint); //绘制圆形
}
}
输出结果:
本人CSDN博客地址:https://blog.csdn.net/tong5956