android drawBitmapMesh()图片旗帜飘扬效果

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

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值