广告
返回顶部
首页 > 资讯 > 精选 >Android使用API实现图像扭曲效果示例
  • 702
分享到

Android使用API实现图像扭曲效果示例

androidapi图像 2023-05-30 22:05:08 702人浏览 独家记忆
摘要

本文实例讲述了Android使用api实现图像扭曲效果。分享给大家供大家参考,具体如下:public class BitmapMesh extends GraphicsActivity { @Override protected void

本文实例讲述了Android使用api实现图像扭曲效果。分享给大家供大家参考,具体如下:

public class BitmapMesh extends GraphicsActivity { @Override protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(new SampleView(this)); } private static class SampleView extends View {   //定义常量,指定该图片横向被划分为20格  private static final int WIDTH = 20;   //定义常量,指定该图片纵向上被划分为20格  private static final int HEIGHT = 20;  //记录该图像上包含441个顶点  private static final int COUNT = (WIDTH + 1) * (HEIGHT + 1);  //位图  private final Bitmap mBitmap;  //数组,记录Bitmap上的21*21个点的坐标  private final float[] mVerts = new float[COUNT * 2];   //记录Bitmap上的21*21个点经过扭曲后的坐标  private final float[] mOrig = new float[COUNT * 2];  private final Matrix mMatrix = new Matrix();  private final Matrix mInverse = new Matrix();  private static void setXY(float[] array, int index, float x, float y) {   array[index * 2 + 0] = x;   array[index * 2 + 1] = y;  }  public SampleView(Context context) {   super(context);   setFocusable(true);   //加载图片   mBitmap = BitmapFactory.decodeResource(getResources(), R.raw.beach);   //获取图像的宽度和高度   float w = mBitmap.getWidth();   float h = mBitmap.getHeight();   //构建扭曲数据   int index = 0;   for (int y = 0; y <= HEIGHT; y++) {    float fy = h * y / HEIGHT;    for (int x = 0; x <= WIDTH; x++) {     float fx = w * x / WIDTH;      //初始化orig,verts数组     //初始化,orig,verts两个数组均匀地保存了21 * 21个点的x,y坐标      setXY(mVerts, index, fx, fy);     setXY(mOrig, index, fx, fy);     index += 1;    }   }   //设置平移效果   mMatrix.setTranslate(10, 10);   //实现乱矩阵逆向坐标映射   mMatrix.invert(mInverse);  }  @Override  protected void onDraw(canvas canvas) {   canvas.drawColor(0xFFCCCCCC);   //对matrix的变换应用到canvas上的所有对象.   canvas.concat(mMatrix);      canvas.drawBitmapMesh(mBitmap, WIDTH, HEIGHT, mVerts, 0, null, 0,     null);  }  //根据触摸事件的位置计算verts数组里各元素的值  private void warp(float cx, float cy) {   final float K = 10000;   float[] src = mOrig;   float[] dst = mVerts;   for (int i = 0; i < COUNT * 2; i += 2) {    float x = src[i + 0];    float y = src[i + 1];    float dx = cx - x;    float dy = cy - y;    float dd = dx * dx + dy * dy;     //计算每个坐标点与当前点(cx,cy)之间的距离    float d = FloatMath.sqrt(dd);     //扭曲度,距离当前点(cx,cy)越远,扭曲度越小    float pull = K / (dd + 0.000001f);    pull /= (d + 0.000001f);    //对dst数组(保存bitmap 上21 * 21个点经过扭曲后的坐标)赋值    if (pull >= 1) {     dst[i + 0] = cx;     dst[i + 1] = cy;    } else {      //控制各顶点向触摸事件发生点偏移     dst[i + 0] = x + dx * pull;     dst[i + 1] = y + dy * pull;    }   }  }  private int mLastWarpX = -9999; // don't match a touch coordinate  private int mLastWarpY;  @SuppressLint("ClickableViewAccessibility") @Override  public boolean onTouchEvent(MotionEvent event) {   float[] pt = { event.getX(), event.getY() };   //用当前矩阵改变pts中的值,然后存储在pts中,同上,pts也是存储点的坐标的数组   mInverse.mapPoints(pt);   int x = (int) pt[0];   int y = (int) pt[1];   if (mLastWarpX != x || mLastWarpY != y) {    mLastWarpX = x;    mLastWarpY = y;    warp(pt[0], pt[1]);    invalidate();   }   return true;  } }}

--结束END--

本文标题: Android使用API实现图像扭曲效果示例

本文链接: https://www.lsjlt.com/news/221623.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作