• Android 颜色渲染(七) RadialGradient 环形渲染实现水波纹效果


    利用环形渲染我们可以做到什么? 其实很多都是非常常见的,比如上一篇实现的帮帮糖效果, 彩色的热气球,比如这里要讲到的水波纹效果,或者也可以理解为扩散色渲染效果

    首先看一下效果图:

                                                        

    轻触屏幕,即可看到对应的效果,可以看到,实现这种效果,利用RadialGradient ,只需简单几行代码:

    MainActivity:

    package com.tony.testshader;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.view.Menu;
    import android.widget.SeekBar;
    import android.widget.SeekBar.OnSeekBarChangeListener;
    
    public class MainActivity extends Activity  implements OnSeekBarChangeListener{
    
         private WaterRipplesView waterRipplesView;
         
         
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            
            waterRipplesView = new WaterRipplesView(this);
            setContentView(waterRipplesView);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
      
    
    }


    WaterRipplesView:

    package com.tony.testshader;
    
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.BitmapShader;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.RadialGradient;
    import android.graphics.Shader;
    import android.graphics.drawable.BitmapDrawable;
    import android.graphics.drawable.ShapeDrawable;
    import android.graphics.drawable.shapes.OvalShape;
    import android.util.AttributeSet;
    import android.util.DisplayMetrics;
    import android.view.MotionEvent;
    import android.view.View;
    /**
     * 水波纹效果
     * @author tony
     *
     */
    
     public class WaterRipplesView extends View {
    
    	Shader mBitmapShader = null;
    	Bitmap mBitmapPn = null;
    	Paint mPaint = null;
    	Shader mRadialGradient = null;
    	Canvas mCanvas = null;
    	ShapeDrawable mShapeDrawable = null;
    
    	public WaterRipplesView(Context context) {
    		super(context);
    
    		// 初始化工作
    		Bitmap bitmapTemp = ((BitmapDrawable) getResources().getDrawable(
    				R.drawable.leaf)).getBitmap();
    		DisplayMetrics dm = getResources().getDisplayMetrics();
    		// 创建与当前使用的设备窗口大小一致的图片
    		mBitmapPn = Bitmap.createScaledBitmap(bitmapTemp, dm.widthPixels,
    				dm.heightPixels, true);
    		// 创建BitmapShader object
    		mBitmapShader = new BitmapShader(mBitmapPn, Shader.TileMode.REPEAT,
    				Shader.TileMode.MIRROR);
    		mPaint = new Paint();
    	}
    
    	public WaterRipplesView(Context context, AttributeSet attrs) {
    		super(context, attrs);
    	}
    
    	@Override
    	protected void onDraw(Canvas canvas) {
    		// TODO Auto-generated method stub
    		super.onDraw(canvas);
    
    		// 将图片裁剪为椭圆型
    		// 创建ShapeDrawable object,并定义形状为椭圆
    		mShapeDrawable = new ShapeDrawable(new OvalShape());// OvalShape:椭圆
    		// 设置要绘制的椭圆形的东西为ShapeDrawable图片
    		mShapeDrawable.getPaint().setShader(mBitmapShader);
    		// 设置显示区域
    		mShapeDrawable.setBounds(0, 0, mBitmapPn.getWidth(),
    				mBitmapPn.getHeight());
    		// 绘制ShapeDrawable
    		mShapeDrawable.draw(canvas);
    		if (mRadialGradient != null) {
    			mPaint.setShader(mRadialGradient);
    			canvas.drawCircle(0, 0, 1000, mPaint);
    		}
    
    	}
    
    	// @覆写触摸屏事件
    	public boolean onTouchEvent(MotionEvent event) {
    		// @设置alpha通道(透明度)
    		mPaint.setAlpha(400);
    		mRadialGradient = new RadialGradient(event.getX(), event.getY(), 48,
    				new int[] { Color.WHITE, Color.TRANSPARENT },null, Shader.TileMode.REPEAT);
    		// @重绘
    		postInvalidate();
    		return true;
    	}
    
    }



  • 相关阅读:
    sas中一些小的选项的含义
    C++变量学习点
    sas,log,output,ods输出管理(html output_object output_statement)
    matlab统计函数
    sas条件判断语句where,if的区别,以及where选项
    sas数组,数组的语法与一些特殊定义,获取维度大小
    sas赋值语句,累加语句,keep,drop,rename,(retain/sum statement)
    解决Xcode 4.3.2的"Could not insert new outlet connection"问题
    网络数据的XML解析
    将UIView中的图像保存到相册
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3293779.html
Copyright © 2020-2023  润新知