您的位置:首页 > 其它

Shadow阴影和ColorFilter颜色过滤

2015-09-16 17:56 453 查看

shadow:

由Paint的setShadowLayer()方法实现。其方法为:
public void setShadowLayer(float radius, float dx, float dy, int shadowColor)

ColorFilter:

颜色过滤器的用途是用paint修改每个像素的颜色。
Android包含三个ColorFilter:ColorMatrixColorFilter  可以指定一个4×5的ColorMatrix并将其应用到一个Paint中。ColorMatrixes通常在程序中用于对图像进行处理 ,而且由于它们支持使用矩阵相乘的方法来执行链接转换,所以它们很有用。LightingColorFilter  乘以第一个颜色的RGB通道,然后加上第二个颜色。每一次转换的结果都限制在0到255之间。PorterDuffColorFilter  可以使用数字图像合成的16条Porter-Duff 规则中的任意一条来向Paint应用一个指定的颜色。一般常用的是PorterDuffColorFilter  。
代码:
public class MyActivity extends Activity {    private ImageView image,image2,image3;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        image = (ImageView) findViewById(R.id.image);        image2 = (ImageView) findViewById(R.id.image2);        image3 = (ImageView) findViewById(R.id.image3);        int width = (int) (getResources().getDisplayMetrics().density * 200);        Bitmap bitmap = Bitmap.createBitmap(width,width, Bitmap.Config.ARGB_8888);        Canvas canvas = new Canvas(bitmap);        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);        //画边框        paint.setColor(Color.BLACK);        paint.setStyle(Paint.Style.STROKE);        canvas.drawRect(new Rect(0, 0, width, width), paint);        paint.setStyle(Paint.Style.FILL);        paint.setShadowLayer(20, 0, 0, Color.BLUE);     //设置阴影        paint.setColor(Color.GREEN);        canvas.drawRect(new Rect(100, 100, 300, 300), paint);        image.setImageBitmap(bitmap);        //===================================        Bitmap bitmap2 = Bitmap.createBitmap(width,width, Bitmap.Config.ARGB_8888);        Canvas canvas2 = new Canvas(bitmap2);        Paint paint2 = new Paint(Paint.ANTI_ALIAS_FLAG);        //画边框        paint2.setColor(Color.BLACK);        paint2.setStyle(Paint.Style.STROKE);        canvas2.drawRect(new Rect(0, 0, width, width), paint2);        paint2.setStyle(Paint.Style.FILL);        paint2.setShadowLayer(100, 100, 20, Color.BLUE);    //设置阴影        paint2.setColor(Color.GREEN);        canvas2.drawRect(new Rect(100, 100, 300, 300), paint2);        image2.setImageBitmap(bitmap2);        //===============================        Bitmap bitmap3 = Bitmap.createBitmap(width,width, Bitmap.Config.ARGB_8888);        Bitmap bitmapIC = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher).copy(Bitmap.Config.ARGB_8888,false);        Canvas canvas3 = new Canvas(bitmap3);        Paint paint3 = new Paint();        //画边框        paint3.setColor(Color.BLACK);        paint3.setStyle(Paint.Style.STROKE);        canvas3.drawRect(new Rect(0, 0, width-1, width), paint3);        paint3.setColorFilter(new PorterDuffColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_IN)); //PorterDuff.Mode.SRC_IN取两层绘制交集。显示上层。        canvas3.drawBitmap(bitmapIC,0,0,paint3);        image3.setImageBitmap(bitmap3);    }
效果图:
注意:对于setShadowLayer,由于硬件加速不支持该方法,所以对于自定义view中使用该api,请设置setLayerType(LAYER_TYPE_SOFTWARE,null);
可以参考这个链接http://stackoverflow.com/questions/17410195/setshadowlayer-android-api-differences
源码:http://yunpan.cn/cma2g7yBzBb4y (提取码:18fa)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: