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;
	}

}


 


 

昵    称:
验证码:

相关文档: