Api Demo - .graphics(7)

// 关键字:Porter-Duff

package com.example.android.apis.graphics;

import com.example.android.apis.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.*;
import android.graphics.drawable.*;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.*;

public class ColorFilters extends GraphicsActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new SampleView(this));
        
    }
    
    private static class SampleView extends View {
        private Activity mActivity;
        private Drawable mDrawable;
        private Drawable[] mDrawables;
        private Paint mPaint;
        private Paint mPaint2;
        private float mPaintTextOffset;
        private int[] mColors;
        private PorterDuff.Mode[] mModes;
        private int mModeIndex;

        private static void addToTheRight(Drawable curr, Drawable prev) {
            Rect r = prev.getBounds();
            int x = r.right + 12;
            int center = (r.top + r.bottom) >> 1;
            int h = curr.getIntrinsicHeight();
            int y = center - (h >> 1);
            
            curr.setBounds(x, y, x + curr.getIntrinsicWidth(), y + h);
        }

        public SampleView(Activity activity) {
            super(activity);
            mActivity = activity;
            Context context = activity;
            setFocusable(true);
            
            mDrawable = context.getResources().getDrawable(R.drawable.btn_default_normal);
            mDrawable.setBounds(0, 0, 150, 48);
            mDrawable.setDither(false);//

            int[] resIDs = new int[] {
                R.drawable.btn_circle_normal,
                R.drawable.btn_check_off,
                R.drawable.btn_check_on
            };
            mDrawables = new Drawable[resIDs.length];//根据ID创建
            Drawable prev = mDrawable;
            for (int i = 0; i < resIDs.length; i++) {
                mDrawables[i] = context.getResources().getDrawable(resIDs[i]);
                mDrawables[i].setDither(true);
                addToTheRight(mDrawables[i], prev);
                prev = mDrawables[i];
            }

            mPaint = new Paint();
            mPaint.setAntiAlias(true);
            mPaint.setTextSize(16);
            mPaint.setTextAlign(Paint.Align.CENTER);
            
            mPaint2 = new Paint(mPaint);//根据已有画笔创建
            mPaint2.setAlpha(64);
            
            Paint.FontMetrics fm = mPaint.getFontMetrics();
            mPaintTextOffset = (fm.descent + fm.ascent) * 0.5f;//(字的顶部+底部)/2
            
            mColors = new int[] {
                0,
                0xCC0000FF,
                0x880000FF,
                0x440000FF,
                0xFFCCCCFF,
                0xFF8888FF,
                0xFF4444FF,
            };

            mModes = new PorterDuff.Mode[] {
                PorterDuff.Mode.MULTIPLY,
                PorterDuff.Mode.DST,
                PorterDuff.Mode.DST_ATOP,
                PorterDuff.Mode.DST_IN,
                PorterDuff.Mode.DST_OUT,
                PorterDuff.Mode.DST_OVER,
                PorterDuff.Mode.LIGHTEN,
                PorterDuff.Mode.SCREEN,
                PorterDuff.Mode.SRC,
                PorterDuff.Mode.SRC_ATOP,
                PorterDuff.Mode.SRC_IN,
                PorterDuff.Mode.SRC_OUT,
                PorterDuff.Mode.SRC_OVER,
                PorterDuff.Mode.XOR
                /*
                 * Porter-Duff 操作是 1 组 12 项用于描述数字图像合成的基本手法, 
                 * 通过组合使用 Porter-Duff 操作,可完成任意 2D图像的合成。
                 * CLEAR   源像素清除目标像素
                 * SRC     源像素覆盖目标像素和空像素
                 * DST     源像素不影响目标像素
                 * SRC_OVER 源像素和目标像素混合,而且覆盖空像素
                 * DST_OVER 源像素不影响目标像素,并且不覆盖空像素
                 * SRC_IN   源像素覆盖目标像素
                 * SRC_OUT   源像素清除目标像素,并且覆盖空像素
                 * DST_IN   源像素的透明值修改目标像素的透明值
                 * DST_OUT   源像素的透明值补充修改目标像素的透明值
                 * SRC_ATOP  源像素和目标像素相混合 
                 * DST_ATOP   源像素的透明值修改目标像素的透明值。源像素覆盖空像素
                 * XOR      源像素的透明度值不从修改目标像素的透明度值。源像素覆盖空像素。
                 */
            };
            mModeIndex = 0;
            
            updateTitle();
        }
        
        private void swapPaintColors() {
            if (mPaint.getColor() == 0xFF000000) {
                mPaint.setColor(0xFFFFFFFF);
                mPaint2.setColor(0xFF000000);
            } else {
//                mPaint.setColor(0xFF000000);
//                mPaint2.setColor(0xFFFFFFFF);
            }
            mPaint2.setAlpha(64);
        }
        
        private void updateTitle() {
            mActivity.setTitle(mModes[mModeIndex].toString());
        }
        
        private void drawSample(Canvas canvas, ColorFilter filter) {
            Rect r = mDrawable.getBounds();
            float x = (r.left + r.right) * 0.5f;
            float y = (r.top + r.bottom) * 0.5f - mPaintTextOffset;

            mDrawable.setColorFilter(filter);
            mDrawable.draw(canvas);
            canvas.drawText("Label", x+1, y+1, mPaint2);
//            canvas.drawText("Label", x, y, mPaint);
            
            for (Drawable dr : mDrawables) {
                dr.setColorFilter(filter);
                dr.draw(canvas);
            }
        }
        
        @Override protected void onDraw(Canvas canvas) {
            canvas.drawColor(0xFFCCCCCC);            

            canvas.translate(8, 12);
            for (int color : mColors) {
                ColorFilter filter;
                if (color == 0) {
                    filter = null;
                } else {
                    filter = new PorterDuffColorFilter(color,
                                                       mModes[mModeIndex]);
                   
                }
                drawSample(canvas, filter);
                canvas.translate(0, 55);
            }
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            float x = event.getX();
            float y = event.getY();
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    break;
                case MotionEvent.ACTION_MOVE:
                    break;
                case MotionEvent.ACTION_UP:
                    // update mode every other time we change paint colors
                    if (mPaint.getColor() == 0xFFFFFFFF) {
                        mModeIndex = (mModeIndex + 1) % mModes.length;
                        updateTitle();
                    }
                    swapPaintColors();
                    invalidate();
                    break;
            }
            return true;
        }
    }
}

 



 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值