• setShadpwLayer实现阴影效果


    package com.loaderman.customviewdemo;
    
    import android.content.Context;
    import android.graphics.*;
    import android.util.AttributeSet;
    import android.view.View;
    
    
    public class ShadowLayerView extends View {
        private Paint mPaint = new Paint();
        private Bitmap mDogBmp;
        private int mRadius = 1, mDx = 10, mDy = 10;
        private boolean mSetShadow = true;
    
        public ShadowLayerView(Context context) {
            super(context);
            init();
        }
    
        public ShadowLayerView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        public ShadowLayerView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            init();
        }
    
        private void init() {
            setLayerType(LAYER_TYPE_SOFTWARE, null);
            mPaint.setColor(Color.BLACK);
            mPaint.setTextSize(25);
            mDogBmp = BitmapFactory.decodeResource(getResources(), R.drawable.dog);
        }
    
    
        public void changeRadius() {
            mRadius++;
            postInvalidate();
        }
    
        public void changeDx() {
            mDx += 5;
            postInvalidate();
        }
    
        public void changeDy() {
            mDy += 5;
            postInvalidate();
        }
    
        public void setShadow(boolean showShadow) {
            mSetShadow = showShadow;
            postInvalidate();
        }
    
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
    
            canvas.drawColor(Color.WHITE);
    
            if (mSetShadow) {
                mPaint.setShadowLayer(mRadius, mDx, mDy, Color.GRAY);
            } else {
                mPaint.clearShadowLayer();
            }
    
            canvas.drawText("启舰", 100, 100, mPaint);
    
            canvas.drawCircle(200, 200, 50, mPaint);
    
            canvas.drawBitmap(mDogBmp, null, new Rect(200, 300, 200 + mDogBmp.getWidth(), 300 + mDogBmp.getHeight()), mPaint);
        }
    }
    package com.loaderman.customviewdemo;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    
    public class MainActivity extends Activity  implements View.OnClickListener{
    
        private ShadowLayerView mShadowLayerView;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mShadowLayerView = (ShadowLayerView)findViewById(R.id.shadowlayerview);
            findViewById(R.id.radius_btn).setOnClickListener(this);
            findViewById(R.id.dx_btn).setOnClickListener(this);
            findViewById(R.id.dy_btn).setOnClickListener(this);
            findViewById(R.id.clear_btn).setOnClickListener(this);
            findViewById(R.id.show_btn).setOnClickListener(this);
        }
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.radius_btn:
                    mShadowLayerView.changeRadius();
                    break;
                case R.id.dx_btn:
                    mShadowLayerView.changeDx();;
                    break;
                case R.id.dy_btn:
                    mShadowLayerView.changeDy();
                    break;
                case R.id.clear_btn:
                    mShadowLayerView.setShadow(false);
                    break;
                case R.id.show_btn:
                    mShadowLayerView.setShadow(true);
                    break;
            }
        }
    }

    效果:


    setShadowLayer(float radius, float dx, float dy, int shadowColor)
    radius 模糊半径
    dx和dy偏移距离
    shadowColor绘制阴影的画笔颜色,对图片无效
  • 相关阅读:
    「Vue」nrm
    「Vue」路由
    「Vue」父子组件之间的传值及调用方法
    「Vue」vue生命周期
    「Vue」自定义指令
    「Vue」自定义按键修饰符
    「Vue」过滤器
    常用断点设置
    BUUCTF 不一样的flag writeup
    好久没有写东西,最近在看逆向相关的东西,做点记录
  • 原文地址:https://www.cnblogs.com/loaderman/p/10212648.html
Copyright © 2020-2023  润新知