Android Paint属性详解

2024-05-15 23:48
文章标签 android 详解 属性 paint

本文主要是介绍Android Paint属性详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 


最近项目中遇到要绘制一个橡皮擦类类似功能的模块。于是总结了下Paint的属性



      1. 图形绘制  画线 画图片等

      列出一些重要的属性
         1. setARGB(int a,int r,int g,int b);  //设置绘制的颜色,a代表透明度,r,g,b代表颜色值。  
          2.setAlpha(int a); //a[0..255] 0:代表全透明  255代表不透明

         3.setStrokeWidth(int);//置画笔的大小

         4.setStrokeCap(Cap cap);//设置笔刷的样式 Paint.Cap.Round ,Cap.SQUARE等分别为圆形、方形

         5.setStyle(Style style);//画笔样式。 Paint.Style.STROKE为一条线。Paint.Style.FILL是从起点开始。一直到终点为止,形成一扇形的绘制区。Paint.Style.FILL_AND_STROKE 为扇形区再加上一个圈
         6. setShadowLayer(float radius ,float dx,float dy,int color);  
        //在图形下面设置阴影层,产生阴影效果,radius为阴影的角度,dx和dy为阴影在x轴和y轴上的距离,color为阴影的颜色  
     
       7.setXfermode(Xfermode xfermode);  
     // 设置图形重叠时的处理方式,如合并,取交集或并集,经常用来制作橡皮的擦除效果  
    
      2.文本绘制
  

      这里文本有人总结了挺好的,不浪费时间 给连接http://hi.baidu.com/982012087/item/4879442440d49dc0a417b645

     3、案例分析

      

package com.example.drawbeforebkg;import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.graphics.AvoidXfermode.Mode;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuffXfermode;
import android.graphics.Xfermode;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;public class MainActivity extends Activity {Path path = new Path();private CircleView mGameView =null;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);mGameView = new CircleView(this);mGameView.setBackgroundDrawable(getResources().getDrawable(R.drawable.logo) );//设置的背景图片 setContentView(mGameView);}class CircleView extends View implements Runnable{int x,y;Canvas canvas;Bitmap resizedBitmap1 ;Paint paint;private int oldx ,oldy;DisplayMetrics  dm = new DisplayMetrics(); CircleView(Context context)//构造函数{super(context);getWindowManager().getDefaultDisplay().getMetrics(dm); // 获取手机屏幕的大小int  view_w = dm.widthPixels; int   view_h = dm.heightPixels; paint=new Paint();paint.setStrokeWidth(20); //当Style为STROKE或者为FILL——OR——STROKEpaint.setColor(Color.RED); //设置颜色值paint.setAlpha(100);//设置透明度paint.setStyle(Paint.Style.STROKE); paint.setStrokeCap(Paint.Cap.ROUND);paint.setStrokeJoin(Paint.Join.ROUND);paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.CLEAR));//paint.setAntiAlias(false);  // paint.setDither(true);//设置是否使用图像抖动处理,会使画笔更平滑饱满。图像更清晰resizedBitmap1 = Bitmap.createBitmap(view_w, view_h, Config.ARGB_8888);canvas = new Canvas(resizedBitmap1);//第一步:构造函数中,将resizedBitmap1作为画布背景new Thread(this).start();}public boolean onTouchEvent(MotionEvent event){x=(int) event.getX();y=(int) (event.getY());switch (event.getAction()) {case MotionEvent.ACTION_DOWN:path.moveTo(x, y);oldx = x;oldy = y;break;case MotionEvent.ACTION_MOVE://path.lineTo(x, y);      path.quadTo(oldx, oldy, x, y);canvas.drawPath(path, paint);//第二步:指定轨迹画图,将path画在画布上          oldx = x;oldy = y;break;case MotionEvent.ACTION_UP:break;default:            break;}return true;
}@Overridepublic  void onDraw(Canvas canvas) {super.onDraw(canvas);canvas.drawBitmap(resizedBitmap1,0,0,paint);//第三步:时刻显示画布上的布景}public void run(){while (!Thread.currentThread().isInterrupted()){try{Thread.sleep(20);}catch (InterruptedException e){Thread.currentThread().interrupt();}// 使用postInvalidate可以直接在线程中更新界面postInvalidate();}}}}


 

 


     

 

这篇关于Android Paint属性详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/993271

相关文章

Linux线程之线程的创建、属性、回收、退出、取消方式

《Linux线程之线程的创建、属性、回收、退出、取消方式》文章总结了线程管理核心知识:线程号唯一、创建方式、属性设置(如分离状态与栈大小)、回收机制(join/detach)、退出方法(返回/pthr... 目录1. 线程号2. 线程的创建3. 线程属性4. 线程的回收5. 线程的退出6. 线程的取消7.

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

MySQL中的LENGTH()函数用法详解与实例分析

《MySQL中的LENGTH()函数用法详解与实例分析》MySQLLENGTH()函数用于计算字符串的字节长度,区别于CHAR_LENGTH()的字符长度,适用于多字节字符集(如UTF-8)的数据验证... 目录1. LENGTH()函数的基本语法2. LENGTH()函数的返回值2.1 示例1:计算字符串

Spring Boot spring-boot-maven-plugin 参数配置详解(最新推荐)

《SpringBootspring-boot-maven-plugin参数配置详解(最新推荐)》文章介绍了SpringBootMaven插件的5个核心目标(repackage、run、start... 目录一 spring-boot-maven-plugin 插件的5个Goals二 应用场景1 重新打包应用

mybatis执行insert返回id实现详解

《mybatis执行insert返回id实现详解》MyBatis插入操作默认返回受影响行数,需通过useGeneratedKeys+keyProperty或selectKey获取主键ID,确保主键为自... 目录 两种方式获取自增 ID:1. ​​useGeneratedKeys+keyProperty(推

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

Linux系统性能检测命令详解

《Linux系统性能检测命令详解》本文介绍了Linux系统常用的监控命令(如top、vmstat、iostat、htop等)及其参数功能,涵盖进程状态、内存使用、磁盘I/O、系统负载等多维度资源监控,... 目录toppsuptimevmstatIOStatiotopslabtophtopdstatnmon

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class