private float getMatrixY() {
float w = (float) ((float) 2 * Math.PI / width);
int y = (int) (18 * Math.sin(w * getMatrixX())) + (height-mLeafHeight)/2;
return y;
}
public LeafView(Context context, AttributeSet attrs) {
super(context, attrs);
mResources = getResources();
bgBitmap = ((BitmapDrawable) mResources.getDrawable(R.drawable.leaf_kuang, null)).getBitmap();
leafBitmap = ((BitmapDrawable) mResources.getDrawable(R.drawable.leaf, null))).getBitmap();
mLeafHeight = leafBitmap.getWidht();
bgPaint = new Paint();
bgPaint.setColor(mResources.getColor(R.color.bg_color));
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
width = w;
height = h;
bgDestRect = new Rect(0, 0 , width, height);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
bgRect = new RectF(0, 0 , width, height);
//添加黄色背景
canvas.drawRect(bgRect, bgPaint);
//添加背景图片
canvas.drawBitmap(bgBitmap, null, bgDestRect, null);
//添加叶子
Matrix matrix = new Matrix();
matrix.postTranslate(getMatriX(), getMatrixY);
canvas.drawBitmap(leafBitmap, new Matrix(), new Paint());
//重复调用onDraw()
postInvalidate();
}
long cycleTime = 5000; //叶子滑动一周的时间5秒
long startTime = 0; //叶子滑动开始时间
private float getMatriX() {
float betweenTime = startTime - System.currentTimeMillis();
//周期结束再加一个cycleTime
if(betweenTime < 0) {
startTime = System.currentTimeMillis() + cycleTime;
betweenTime = cycleTime;
}
//通过时间差计算出叶子的坐标
float fraction = (float) betweenTime / cycleTime;
float x = (int)(width * fraction);
return x;
}
private float getMatrixY() {
float w = (float) ((float) 2 * Math.PI / width);
int y = (int) (18 * Math.sin(w * getMatrixX())) + (height-mLeafHeight)/2;
return y;
}
private int getRotate() {
float scale = ((startTime - System.currentTimeMillis())%cycleTime)/ (float)cycleTime;
int rotate = (int)(scale * 360);
return rotate;
}
public class LeafView extends View {
private Resources mResources;
private Bitmap mLeafBitmap, bgBitmap;
private int width, height;
private int mLeafWidth,mLeafHeight;
private Paint bgPaint;
private RectF bgRect;
private Rect bgDestRect;
public LeafView(Context context, AttributeSet attrs) {
super(context, attrs);
mResources = getResources();
mLeafBitmap = ((BitmapDrawable) mResources.getDrawable(R.drawable.leaf, null)).getBitmap();
mLeafWidth = mLeafBitmap.getWidht();
mLeafHeight = mLeafBitmap.getHeight();
bgBitmap = ((BitmapDrawable) mResources.getDrawable(R.drawable.leaf_kuang, null)).getBitmap();
bgPaint = new Paint();
bgPaint.setColor(mResources.getColor(R.color.bg_color));
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
width = w;
height = h;
bgDestRect = new Rect(0, 0 , width, height);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
bgRect = new RectF(0, 0 , width, height);
//添加黄色白金
canvas.drawRect(bgRect, bgPaint);
//添加背景图片
canvas.drawBitmap(bgBitmap, null, bgDestRect, null);
canvas.save();
Matrix matrix = new Matrix();
//添加滑动
matrix.postTranslate(getMatrixX(), getMatrixY());
//添加旋转
matrix.postRotate(getRotate(), getMatrixX() + mLeafWidth / 2, getMatrixY() + mLeafHeight / 2);
canvas.drawBitmap(mLeafBitmap, matrix, new Paint());
canvas.restore();
postInvalidate();
}
long cycleTime = 5000; //叶子滑动一周的时间5秒
long startTime = 0;
private float getMatrixX() {
float betweenTime = startTime - System.currentTimeMillis();
//周期结束再加一个cycleTime
if(betweenTime < 0) {
startTime = System.currentTimeMillis() + cycleTime;
betweenTime = cycleTime;
}
//通过时间差计算出叶子的坐标
float fraction = (float) betweenTime / cycleTime;
float x = (int)(width * fraction);
return x;
}
private float getMatrixY() {
float w = (float) ((float) 2 * Math.PI / width);
int y = (int) (18 * Math.sin(w * getMatrixX())) + (height-mLeafHeight)/2;
return y;
}
private int getRotate() {
float scale = ((startTime - System.currentTimeMillis())%cycleTime)/ (float)cycleTime;
int rotate = (int)(scale * 360);
return rotate;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有