您的位置:首页 > 其它

自定义组件一

2016-07-11 21:53 246 查看
Paint

类表示画笔,用来描述图形的颜色和风格,如线宽、颜色、透明度和填充效果等信息。

使用Paint类时,需要先创建该类的对象,这可以通过该类提供的构造方法来实现,通常情况下,只需要使用Paint()方法来创建一个使用默认设置Paint对象。

使用Paint可以基本分为三步:

1.通过构造方法创建出一个Paint的实例出来;

2.通过各类set方法对该Paint对象进行设置;

3.通常搭配Canvas来使用Paint

实际使用例子:

首先,在布局中添加ImageView控件

<ImageView
android:id="@+id/imageview"
android:layout_width="300px"
android:layout_height="300px" />

在main中创建画笔

创建一个矢量图bitmap

setContentView(R.layout.activity_main);
//        创建画笔
Paint paint = new Paint();
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SCREEN));
//        创建一个矢量图(宽,高,设置(满饱和度))
Bitmap bitmap = Bitmap.createBitmap(600,600, Bitmap.Config.ARGB_8888);
//        创建一个画布
Canvas canvas = new Canvas(bitmap);
paint.setColor(Color.BLUE);
//        绘图的结果最后都会保留在bitmap中(圆心坐标(x,y),圆弧半径长度)
canvas.drawCircle(100, 100, 100, paint);
//        拿到imageView视图,并设置他的图片为刚才的图片
paint.setColor(Color.YELLOW);
//       使用画笔在画布上画一个矩形(距左,上,右边距左,下边距上)
Rect rect = new Rect(150, 150, 400, 400);
canvas.drawRect(rect, paint);
//        作弧形
       paint.setColor(Color.RED);
RectF rect2= new RectF(200,200,600,600);
//        (相对的矩形,起始度数,移动度数,是否使用画笔,和画笔)
canvas.drawArc(rect2,0,120,false,paint);
paint.setColor(Color.BLACK);
//          线条宽度
       paint.setStrokeWidth(20);
canvas.drawLine(100,100,500,500,paint);
//          paint作文字
       paint.setColor(Color.GREEN);
paint.setTextSize(200);
canvas.drawText("FUCK",100,300,paint);
//         bitmap放入到imageView
ImageView imageView = (ImageView) findViewById(R.id.imageview);
imageView.setImageBitmap(bitmap);

}

}


አֵ
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  canvas 控件