android drawBitmapMesh
16lz
2021-12-04
package com.example.and6;import android.annotation.SuppressLint;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.util.AttributeSet;import android.widget.ImageView;public class MeshView extends ImageView{Bitmap bitmap;//分割成50*50个格int WIDTH=50;int HEIGHT=50;//依次存放每一个点的 x y坐标float[]origs=new float[ ( (WIDTH+1)*(HEIGHT+1) )*2];float[]verts=new float[origs.length];float k;boolean isStart=false;public MeshView(Context context) {super(context);init();}public MeshView(Context context, AttributeSet attrs) {super(context, attrs);init();}public MeshView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init();}@SuppressLint("NewApi")public MeshView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {super(context, attrs, defStyleAttr, defStyleRes);init();}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);flagWave();k+=0.1f;canvas.drawBitmapMesh(bitmap, WIDTH, HEIGHT, verts, 0, null, 0, null);if (isStart) {invalidate();}}public void startAnim() {isStart=true;requestLayout();}public void stopAnim() {isStart=false;}private void init() {bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pic);float bitmapHeight=bitmap.getHeight();float bitmapWidth=bitmap.getHeight();int index=0;for (int y = 0; y <= HEIGHT; y++) {float fy=bitmapHeight*y/HEIGHT;for (int x = 0; x <= WIDTH; x++) {float fx=bitmapWidth*x/WIDTH;origs[index*2+0]=verts[index*2+0]=fx;origs[index*2+1]=verts[index*2+1]=fy;index++;}}}private void flagWave() {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( i*1.0f/WIDTH*2*Math.PI + Math.PI*k);//k 实现周期变化verts[ (j*(WIDTH+1)+i )*2+1 ]=origs[ ( j*WIDTH+i )*2+1 ]+offsetY*100; //100 振幅}}}}
更多相关文章
- 学个明白--Android世界的坐标系
- Android写文件到SDCard的一般过程和代码
- Android写文件到SDCard的一般过程和代码
- Google API应用
- android获取gps坐标
- 【Android的从零单排开发日记】之入门篇(三)——Android目录结构
- Android(安卓)View滑动
- android TextView 容纳不下内容,让字向左滚动的办法
- ArcGIS API for Android(安卓)案例教程 17