android drawBitmapMesh()图片旗帜飘扬效果
2016-05-21 20:47
447 查看
package com.moredo.drawbitamp;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.View;
@SuppressLint(“DrawAllocation”)
public class FlagGril extends View {
private Context mContext;///< 上下文环境
private Bitmap bm;///< 图片
private final int HEIGHT = 100;///< 竖着的网格数
private final int WIDTH = 100;///< 横着的网格数
private float[] orig,verts;///< 原始坐标点,改变后的坐标点
private int A = 30;///< 波动的力度
private float k = 1;
public FlagGril(Context context) {
super(context);
this.mContext = context;
bm = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.gril);
orig = new float[(HEIGHT + 1) * (WIDTH + 1) * 2];
verts = new float[(HEIGHT + 1) * (WIDTH + 1) * 2];
///< 初始化坐标
initOrig();
}
}
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.View;
@SuppressLint(“DrawAllocation”)
public class FlagGril extends View {
private Context mContext;///< 上下文环境
private Bitmap bm;///< 图片
private final int HEIGHT = 100;///< 竖着的网格数
private final int WIDTH = 100;///< 横着的网格数
private float[] orig,verts;///< 原始坐标点,改变后的坐标点
private int A = 30;///< 波动的力度
private float k = 1;
public FlagGril(Context context) {
super(context);
this.mContext = context;
bm = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.gril);
orig = new float[(HEIGHT + 1) * (WIDTH + 1) * 2];
verts = new float[(HEIGHT + 1) * (WIDTH + 1) * 2];
///< 初始化坐标
initOrig();
}
private void initOrig() { ///< 获取位图的宽高 float width = bm.getWidth(); float height = bm.getHeight(); int index = 0 ; for(int y = 0 ; y <= HEIGHT ;y++) { ///< 纵坐标 float fy = height * y / HEIGHT; for(int x = 0;x <= WIDTH ; x++) { ///< 横坐标 float fx = width * x / WIDTH ; orig[index * 2 + 0] = verts[index * 2 +0] = fx; ///< 在这里人为将坐标加100是为了让图像下移,避免扭曲后被屏幕遮挡 orig[index * 2 + 1] = verts[index * 2 + 1] = fy + 100; index += 1; } } } /** * @fun 波动 * 改变纵坐标的值,横坐标不变 */ private void flagWavr() { for(int j = 0;j <= HEIGHT ;j++) { for(int i = 0 ; i <= WIDTH ;i++) { ///< 横坐标不变 verts[(j * (WIDTH + 1) + i) * 2 + 0] += 0; float offsetY = (float) Math.sin((float)i / WIDTH * 2 * Math.PI + Math.PI * k); verts[(j * (WIDTH + 1) + i) * 2 + 1] =orig[(j * WIDTH + i) * 2 + 1] +offsetY *A; } } } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub flagWavr(); k += 0.1F; canvas.drawBitmapMesh(bm, WIDTH, HEIGHT, verts, 0, null, 0, null); invalidate(); // canvas.drawBitmap(bm, 0, 0, new Paint()); }
}
相关文章推荐
- service服务的生命周期
- android 代码设置、打开wifi热点及热点的连接
- java/android 设计模式学习笔记(3)---工厂方法模式
- android自动连接接入自己WIfi热点的设备的IP地址
- Android EditText在ScrollView中被输入法遮挡
- Android EditText在ScrollView中被输入法遮挡
- Android JSON解析库Gson和Fast-json的使用对比和图书列表小案例
- Android JSON解析库Gson和Fast-json的使用对比和图书列表小案例
- Android 物理按键的监听Back,Menu和音量(不包括home键,锁屏键,以及长按菜单的监听
- Android 解决Listview条目里面有Edittext数据错乱问题
- 在Android开发中使用MVP模式
- (Android Studio)自定义 ProgressBar (二)
- 探寻Android中MVP设计模式
- android课程学习:打招呼案例
- [Android] 布局优化
- 带你一步步实现android引导页面
- 【Android学习】案例学开发,天气记事本项目学习总结。RxJava+Retrofit2+greenDAO
- 136_android布局优化措施
- Android 如何监听输入法关闭事件
- Android 如何监听输入法关闭事件