Emboss edges of a image shape showing the depth in android

前端 未结 3 1517
予麋鹿
予麋鹿 2021-02-06 09:18

I want to show 3D embossed look and feel as shown in following image. I used EmbossMaskFilter but cannot get it to show the effect (see code below). Is there a different way to

3条回答
  •  走了就别回头了
    2021-02-06 09:33

    I think you will need a larger blur radius and smaller values for ambient light and specular highlight.

    I played around with this by creating a UI to tweak parameters.

    Here is the code I used, so you can try it out. I used a simple blue rectangle, but you should be able to easily plug in whatever image you want to draw.

    MainActivity.java:

    import android.graphics.EmbossMaskFilter;
    import android.os.Bundle;
    import android.support.v7.app.ActionBarActivity;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.SeekBar;
    import android.widget.TextView;
    
    public class MainActivity extends ActionBarActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            final DrawView drawView = (DrawView) findViewById(R.id.drawView);
    
            final TextView ambientLightTextView = (TextView) findViewById(R.id.ambientLightTextView);
    
            SeekBar ambientLightSeekBar = (SeekBar) findViewById(R.id.ambientLightSeekBar);
            ambientLightSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                    float value = (float) progress / 1000F;
                    ambientLightTextView.setText(Float.toString(value));
                    drawView.setAmbientLight(value);
                }
    
                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {}
                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {}
            });
    
            final TextView specularHighlightTextView = (TextView) findViewById(R.id.specularHighlightTextView);
    
            SeekBar specularHighlightSeekBar = (SeekBar) findViewById(R.id.specularHighlightSeekBar);
            specularHighlightSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                    float value = (float) progress / 100f;
                    specularHighlightTextView.setText(Float.toString(value));
                    drawView.setSpecularHighlight(value);
                }
    
                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {}
                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {}
            });
    
            final TextView blurRadiusTextView = (TextView) findViewById(R.id.blurRadiusTextView);
    
            SeekBar blurRadiusSeekBar = (SeekBar) findViewById(R.id.blurRadiusSeekBar);
            blurRadiusSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                    float value = (float) progress / 5F;
                    blurRadiusTextView.setText(Float.toString(value));
                    drawView.setBlurRadius(value);
                }
    
                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {}
                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {}
            });
        }
    
    }
    

    DrawView.java:

    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.EmbossMaskFilter;
    import android.graphics.Paint;
    import android.graphics.Rect;
    import android.util.AttributeSet;
    import android.view.View;
    
    public class DrawView extends View {
    
        private Rect rect = new Rect();
    
        private Paint paint;
    
        private EmbossMaskFilter filter;
    
        private float[] mLightSource = new float[] {2, 5, 5};
    
        private float mAmbientLight = 0.5F;
    
        private float mSpecularHighlight = 8F;
    
        private float mBlurRadius = 15f;
    
        public DrawView(Context context) {
            super(context);
            init();
        }
    
        public DrawView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        public DrawView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init();
        }
    
        private void init() {
    
            setLayerType(LAYER_TYPE_SOFTWARE, null);
    
            filter = new EmbossMaskFilter(mLightSource, mAmbientLight, mSpecularHighlight, mBlurRadius);
    
            paint = new Paint();
            paint.setColor(0xFF0000FF);
            paint.setStyle(Paint.Style.FILL);
            paint.setAntiAlias(true);
            paint.setDither(true);
            paint.setMaskFilter(filter);
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
    
            rect.left = getWidth() / 3;
            rect.right = rect.left * 2;
            rect.top = getHeight() / 3;
            rect.bottom = rect.top * 2;
    
            canvas.drawRect(rect, paint);
        }
    
        public void setAmbientLight(float value) {
            if (value > 1.0F) value = 1.0F;
            if (value < 0) value = 0;
            mAmbientLight = value;
            filter = new EmbossMaskFilter(mLightSource, mAmbientLight, mSpecularHighlight, mBlurRadius);
            paint.setMaskFilter(filter);
            invalidate();
        }
    
        public void setSpecularHighlight(float value) {
            mSpecularHighlight = value;
            filter = new EmbossMaskFilter(mLightSource, mAmbientLight, mSpecularHighlight, mBlurRadius);
            paint.setMaskFilter(filter);
            invalidate();
        }
    
        public void setBlurRadius(float value) {
            mBlurRadius = value;
            filter = new EmbossMaskFilter(mLightSource, mAmbientLight, mSpecularHighlight, mBlurRadius);
            paint.setMaskFilter(filter);
            invalidate();
        }
    }
    

    activity_main.xml:

    
    
        
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
    
    

提交回复
热议问题