zoukankan      html  css  js  c++  java
  • Android Paint和Color类

    要绘图,首先得调整画笔,待画笔调整好之后,再将图像绘制到画布上,这样才可以显示在手机屏幕上。Android 中的画笔是 Paint类,Paint 中包含了很多方法对其属性进行设置,主要方法如下: 

       setAntiAlias: 设置画笔的是否抗锯齿,需要配合setFlags (Paint.ANTI_ALIAS_FLAG) 来帮助消除锯齿使其边缘更平滑。 
       setColor: 设置画笔颜色 
       setARGB:  设置画笔的a,r,p,g值。 
       setAlpha:  设置Alpha值 
       setTextSize: 设置字体尺寸。 
       setStyle:  设置画笔风格,空心或者实心。 
       setStrokeWidth: 设置空心的边框宽度。 
       getColor:  得到画笔的颜色 

       getAlpha:  得到画笔的Alpha值。 

    实例

    public class GameView extends View implements Runnable {
    
    // 声明Paint对象
    private Paint mPaint = null;
    
    public GameView(Context context) {
    super(context);
    // 构建对象
    mPaint = new Paint();
    
    // 开启线程
    new Thread(this).start();
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    
    // 设置Paint为无锯齿
    mPaint.setAntiAlias(true);
    
    // 设置Paint的颜色
    mPaint.setColor(Color.RED);
    mPaint.setColor(Color.BLUE);
    mPaint.setColor(Color.YELLOW);
    mPaint.setColor(Color.GREEN);
    // 同样是设置颜色
    mPaint.setColor(Color.rgb(255, 0, 0));
    
    // 提取颜色
    Color.red(0xcccccc);
    Color.green(0xcccccc);
    
    // 设置paint的颜色和Alpha值(a,r,g,b)
    mPaint.setAlpha(220);
    
    // 这里可以设置为另外一个paint对象
    // mPaint.set(new Paint());
    // 设置字体的尺寸
    mPaint.setTextSize(14);
    
    // 设置paint的风格为“空心”
    // 当然也可以设置为"实心"(Paint.Style.FILL)
    mPaint.setStyle(Paint.Style.STROKE);
    
    // 设置“空心”的外框的宽度
    mPaint.setStrokeWidth(5);
    
    // 得到Paint的一些属性 颜色、Alpha值、外框的宽度、字体尺寸
    Log.i("TAG", "paint Color------>" + mPaint.getColor());
    Log.i(TAG, "paint Alpha------->" + mPaint.getAlpha());
    Log.i("TAG", "paint StrokeWidth--------->" + mPaint.getStrokeWidth());
    Log.i("TAG", "paint TextSize----------->" + mPaint.getTextSize());
    
    // 绘制一空心个矩形
    canvas.drawRect((320 - 80), 20, (320 - 80) / 2 + 80, 20 + 40, mPaint);
    
    // 设置风格为实心
    mPaint.setStyle(Paint.Style.FILL);
    
    mPaint.setColor(Color.GREEN);
    
    // 绘制绿色实心矩形
    canvas.drawRect(0, 20, 40, 20 + 40, mPaint);
    }
    
    // 触笔事件
    public boolean onTouchEvent(MotionEvent event) {
    return true;
    }
    
    // 按键按下事件
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    return true;
    }
    
    // 按键弹起事件
    public boolean onKeyUp(int keyCode, KeyEvent event) {
    return true;
    }
    
    public boolean onKeyMultiple(int KeyCode, int repeatCount, KeyEvent event) {
    return true;
    }
    
    @Override
    public void run() {
    while (!Thread.currentThread().isInterrupted()) {
    try {
    Thread.sleep(100);
    } catch (Exception e) {
    Thread.currentThread().interrupt();
    }
    // 更新界面
    postInvalidate();
    }
    }
    }
    
    
    package eoe.Demo;
    
    import android.app.Activity;
    import android.os.Bundle;
    
    public class Activity01 extends Activity {
    /** Called when the activity is first created. */
    private GameView mGameView;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    setContentView(R.layout.main);
    
    mGameView = new GameView(this);
    
    setContentView(mGameView);
    }
    }

      /** Paint类介绍  

         *   

         * Paint即画笔,在绘图过程中起到了极其重要的作用,画笔主要保存了颜色,  

         * 样式等绘制信息,指定了如何绘制文本和图形,画笔对象有很多设置方法,  

         * 大体上可以分为两类,一类与图形绘制相关,一类与文本绘制相关。         

         *   

         * 1.图形绘制  

         * setARGB(int a,int r,int g,int b);  

         * 设置绘制的颜色,a代表透明度,r,g,b代表颜色值。  

         *   

         * setAlpha(int a);  

         * 设置绘制图形的透明度。  

         *   

         * setColor(int color);  

         * 设置绘制的颜色,使用颜色值来表示,该颜色值包括透明度和RGB颜色。  

         *   

        * setAntiAlias(boolean flag);  

         * 设置是否使用抗锯齿功能,会消耗较大资源,绘制图形速度会变慢。  

         *   

         * setDither(boolean ditherFlag);  

         * 设定是否使用图像抖动处理,会使绘制出来的图片颜色更加平滑和饱满,图像更加清晰  

         *   

         * setFilterBitmap(boolean filterFlag);  

         * 如果该项设置为true,则图像在动画进行中会滤掉对Bitmap图像的优化操作,加快显示  

         * 速度,本设置项依赖于dither和xfermode的设置  

         *   

         * setMaskFilter(MaskFilter maskfilter);  

         * 设置MaskFilter,可以用不同的MaskFilter实现滤镜的效果,如滤化,立体等       *   

         * setColorFilter(ColorFilter colorfilter);  

         * 设置颜色过滤器,可以在绘制颜色时实现不用颜色的变换效果  

         *   

         * setPathEffect(PathEffect effect);  

         * 设置绘制路径的效果,如点画线等  

         *   

         * setShader(Shader shader);  

         * 设置图像效果,使用Shader可以绘制出各种渐变效果  

         *  

         * setShadowLayer(float radius ,float dx,float dy,int color);  

         * 在图形下面设置阴影层,产生阴影效果,radius为阴影的角度,dx和dy为阴影在x轴和y轴上的距离,color为阴影的颜色  

         *   

         * setStyle(Paint.Style style);  

         * 设置画笔的样式,为FILL,FILL_OR_STROKE,或STROKE  

         *   

         * setStrokeCap(Paint.Cap cap);  

         * 当画笔样式为STROKE或FILL_OR_STROKE时,设置笔刷的图形样式,如圆形样式  

         * Cap.ROUND,或方形样式Cap.SQUARE  

         *   

         * setSrokeJoin(Paint.Join join);  

         * 设置绘制时各图形的结合方式,如平滑效果等  

         *   

         * setStrokeWidth(float width);  

         * 当画笔样式为STROKE或FILL_OR_STROKE时,设置笔刷的粗细度  

         *   

         * setXfermode(Xfermode xfermode);  

         * 设置图形重叠时的处理方式,如合并,取交集或并集,经常用来制作橡皮的擦除效果  

         *   

         * 2.文本绘制  

         * setFakeBoldText(boolean fakeBoldText);  

         * 模拟实现粗体文字,设置在小字体上效果会非常差  

         *   

         * setSubpixelText(boolean subpixelText);  

         * 设置该项为true,将有助于文本在LCD屏幕上的显示效果  

         *   

         * setTextAlign(Paint.Align align);  

         * 设置绘制文字的对齐方向  

         *   

       * setTextScaleX(float scaleX);  

        * 设置绘制文字x轴的缩放比例,可以实现文字的拉伸的效果  

         *   

         * setTextSize(float textSize);  

         * 设置绘制文字的字号大小  

         *   

         * setTextSkewX(float skewX);  

         * 设置斜体文字,skewX为倾斜弧度  

         *   

         * setTypeface(Typeface typeface);  

         * 设置Typeface对象,即字体风格,包括粗体,斜体以及衬线体,非衬线体等  

         *   

         * setUnderlineText(boolean underlineText);  

         * 设置带有下划线的文字效果  

         *   

         * setStrikeThruText(boolean strikeThruText);  

         * 设置带有删除线的效果  


  • 相关阅读:
    poj3537--Crosses and Crosses
    poj3480--John
    poj2975--Nim
    poj2960 S-Nim
    poj2505-A multplication game
    Team Queue(POJ 2259)
    将caffemodel文件转换为Matlab可用的数据形式
    Invalid MEX-file: caffe.mexa64 的解决方案
    mongoDB-3.x启用认证
    mongoDB跨平台图形管理工具
  • 原文地址:https://www.cnblogs.com/krislight1105/p/3748351.html
Copyright © 2011-2022 走看看