Android View上绘制图形和图片的总结

近期用了一下View上绘画,在总结一下:

其实都是直接使用sdk中提供的接口

绘制图形的例子

package com.cby.jason.draw2d;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Jason on 2016/2/27.
 */
public class DrawGraphicalView extends View{

    private Paint paint1 = new Paint();
    private Paint paint2 = new Paint();

    public DrawGraphicalView(Context context) {
        super(context);
        initView();
    }

    public DrawGraphicalView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    private void initView() {
        //初始化
        paint1.setColor(Color.RED);
        paint1.setStrokeWidth(10f);
        paint2.setColor(Color.YELLOW);
        paint2.setStrokeWidth(20f);
        paint2.setStyle(Paint.Style.STROKE);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //画像素点
        canvas.drawPoint(100,80,paint1);
        canvas.drawPoint(100, 100, paint2);
        canvas.drawPoints(new float[]{100, 120, 100, 140}, paint1);
        //画线
        canvas.drawLine(10,10,200,10,paint1);
        canvas.drawLine(10, 40, 200, 40, paint2);
        //用线画三角形
        canvas.drawLines(new float[]{160, 100, 80, 250, 160, 100, 240, 250, 80, 250, 240, 250}, paint1);
        //画矩形
        canvas.drawRect(80,300,380,600,paint1);
        canvas.drawRect(40,260,440,640,paint2);
        Rect r = new Rect(100,320,360,580);
        Paint p = new Paint();
        p.setStrokeWidth(10f);
        p.setStyle(Paint.Style.STROKE);
        p.setColor(Color.BLUE);
        canvas.drawRect(r, p);

        //画弧
        canvas.drawArc(480, 300, 880, 600, 15f, 220f, false, paint1);
        canvas.drawArc(40, 660, 440, 1040, 30, 150, true, paint2);
        RectF r1 = new RectF(480, 660, 880, 1040);
        canvas.drawArc(r1, 15f, 220f, true, paint1);

        //画圆
        canvas.drawCircle(600, 120, 100f, paint1);
        //画椭圆
        canvas.drawArc(450, 20, 750, 220, 0, 360, false, paint2);
        //画文字
        canvas.drawText("what are words",66,1200,new Paint(Color.BLACK));
        canvas.drawText("what are words",3,9,66,1250,new Paint(Color.BLACK));
        Paint p1 = new Paint(Color.BLACK);
        p1.setTextSize(25);//设置文字大小
        canvas.drawText("what are words",66,1300,p1);
        //
        canvas.drawOval(150,1300,300,1450,p1);
    }
}
在View上绘制位图的例子:

package com.cby.jason.draw2d;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

import java.io.InputStream;

/**
 * Created by Jason on 2016/2/28.
 */
public class DrawBitmapView extends View {

    private Bitmap mBitmap;
    private Bitmap bm;
    private InputStream is;
    private Context mContext;

    public DrawBitmapView(Context context) {
        super(context);
        initView(context);
    }

    public DrawBitmapView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context){
        mContext = context;
        is = context.getResources().openRawResource(R.raw.ic_folders);
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inSampleSize = 2;
        mBitmap = BitmapFactory.decodeStream(is, null, opt);
        bm = drawableToBitamp(mContext.getResources().getDrawable(R.drawable.cross));
    }

    int disgree= 0;
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(mBitmap, 100, 100, null);

        //drawableToBitamp(mContext.getResources().getDrawable(R.drawable.ic_launcher));
        Drawable d = mContext.getResources().getDrawable(R.drawable.ic_launcher);
        int w = d.getIntrinsicWidth();
        int h = d.getIntrinsicHeight();
        d.setBounds(300, 200, 300 + w, 200 + h);
        d.draw(canvas);

        Matrix matrix = new Matrix();
        if(disgree > 360){
            disgree = 0;
        }
        disgree++;
        float px;
        float py;
        px = bm.getWidth()/2;
        py = bm.getHeight()/2;
        matrix.setRotate(disgree, px, py);
        //canvas.setMatrix(matrix);
        //canvas.drawBitmap(bm,550,550,null);

        canvas.drawBitmap(bm,matrix,null);
        invalidate();

    }


    private Bitmap drawableToBitamp(Drawable drawable)
    {
        Bitmap bitmap;
        int w = drawable.getIntrinsicWidth();
        int h = drawable.getIntrinsicHeight();
        System.out.println("Drawable转Bitmap");
        Bitmap.Config config =
                drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                        : Bitmap.Config.RGB_565;
        bitmap = Bitmap.createBitmap(w,h,config);
        //注意,下面三行代码要用到,否在在View或者surfaceview里的canvas.drawBitmap会看不到图
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, w, h);
        drawable.draw(canvas);
        return bitmap;
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值