初始界面是没有轮回眼效果的,当触摸屏幕上时,就会出现该轮回眼,随着你触摸点的移动而移动。
如下是程序代码:
package radialgradient.peien;
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.graphics.drawable.shapes.RectShape;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
public class PeienGradient extends View{
Shader mBitmapShader = null;
//@代表佩恩的图片
Bitmap mBitmapPn = null;
//@绘制的Paint
Paint mPaint = null;
//@轮回眼渐变
Shader mRadialGradient = null;
//@画布
Canvas mCanvas = null;
ShapeDrawable mShapeDrawable = null;
public PeienGradient(Context context) {
super(context);
// TODO Auto-generated constructor stub
//@初始化工作
Bitmap bitmapTemp = ((BitmapDrawable)getResources().getDrawable(R.drawable.peien)).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();
}
//@覆写onDraw方法
public void onDraw(Canvas canvas){
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.BLACK,Color.rgb(166, 102, 244)},/*Color.rgb(166, 102, 244)*/
null,Shader.TileMode.REPEAT);
//@重绘
postInvalidate();
return true;
}
}
在主activity类中
setContentView(new PeienGradient(this));
表示在这个activity界面中用改派生的View类来显示。
这个程序在我的博客园中也有
欢迎大家访问我的博客园:/cmperple/articles/Peien-lunhuieye-android-made.html