请帮我解决这个问题.
谢谢.
码:
BitmapMesh活动:
import androID.content.Context;import androID.graphics.Bitmap;import androID.graphics.BitmapFactory;import androID.graphics.Canvas;import androID.graphics.Matrix;import androID.os.Bundle;import androID.util.floatMath;import androID.vIEw.MotionEvent;import androID.vIEw.VIEw;public class BitmapMesh extends GraphicsActivity { @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(new SampleVIEw(this)); } private static class SampleVIEw extends VIEw { private static final int WIDTH = 20; private static final int HEIGHT = 20; private static final int COUNT = (WIDTH + 1) * (HEIGHT + 1); private final Bitmap mBitmap; private final float[] mVerts = new float[COUNT * 2]; 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.drawable.image1); float w = mBitmap.getWIDth(); float h = mBitmap.getHeight(); // construct our mesh 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; setXY(mVerts,index,fx,fy); setXY(mOrig,fy); index += 1; } } mMatrix.setTranslate(10,10); mMatrix.invert(mInverse); } @OverrIDe protected voID onDraw(Canvas canvas) { canvas.drawcolor(0xFFCCCCCC); canvas.concat(mMatrix); canvas.drawBitmapMesh(mBitmap,WIDTH,HEIGHT,mVerts,null,null); } 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; float d = floatMath.sqrt(dd); float pull = K / (dd + 0.000001f); pull /= (d + 0.000001f); // androID.util.Log.d("skia","index " + i + " dist=" + d + // " pull=" + pull); 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; @OverrIDe public boolean ontouchEvent(MotionEvent event) { float[] pt = { event.getX(),event.getY() }; 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; } }}
图形活动:
import androID.app.Activity;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;class GraphicsActivity extends Activity { // set to true to test Picture private static final boolean TEST_PICTURE = false; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @OverrIDe public voID setContentVIEw(VIEw vIEw) { if (TEST_PICTURE) { VIEwGroup vg = new PictureLayout(this); vg.addVIEw(vIEw); vIEw = vg; } super.setContentVIEw(vIEw); }}
PictureLayout.java
import androID.content.Context;import androID.graphics.Canvas;import androID.graphics.Picture;import androID.graphics.Rect;import androID.graphics.drawable.Drawable;import androID.util.AttributeSet;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.vIEw.VIEwParent;class PictureLayout extends VIEwGroup { private final Picture mPicture = new Picture(); public PictureLayout(Context context) { super(context); } public PictureLayout(Context context,AttributeSet attrs) { super(context,attrs); } @OverrIDe public voID addVIEw(VIEw child) { if (getChildCount() > 1) { throw new IllegalStateException( "PictureLayout can host only one direct child"); } super.addVIEw(child); } @OverrIDe public voID addVIEw(VIEw child,int index) { if (getChildCount() > 1) { throw new IllegalStateException( "PictureLayout can host only one direct child"); } super.addVIEw(child,index); } @OverrIDe public voID addVIEw(VIEw child,LayoutParams params) { if (getChildCount() > 1) { throw new IllegalStateException( "PictureLayout can host only one direct child"); } super.addVIEw(child,params); } @OverrIDe public voID addVIEw(VIEw child,params); } @OverrIDe protected LayoutParams generateDefaultLayoutParams() { return new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT); } @OverrIDe protected voID onMeasure(int wIDthMeasureSpec,int heightmeasureSpec) { final int count = getChildCount(); int maxHeight = 0; int maxWIDth = 0; for (int i = 0; i < count; i++) { final VIEw child = getChildAt(i); if (child.getVisibility() != GONE) { measureChild(child,wIDthMeasureSpec,heightmeasureSpec); } } maxWIDth += getpaddingleft() + getpaddingRight(); maxHeight += getpaddingtop() + getpaddingBottom(); Drawable drawable = getBackground(); if (drawable != null) { maxHeight = Math.max(maxHeight,drawable.getMinimumHeight()); maxWIDth = Math.max(maxWIDth,drawable.getMinimumWIDth()); } setMeasuredDimension(resolveSize(maxWIDth,wIDthMeasureSpec),resolveSize(maxHeight,heightmeasureSpec)); } private voID drawPict(Canvas canvas,int x,int y,int w,int h,float sx,float sy) { canvas.save(); canvas.translate(x,y); canvas.clipRect(0,w,h); canvas.scale(0.5f,0.5f); canvas.scale(sx,sy,h); canvas.drawPicture(mPicture); canvas.restore(); } @OverrIDe protected voID dispatchDraw(Canvas canvas) { super.dispatchDraw(mPicture.beginRecording(getWIDth(),getHeight())); mPicture.endRecording(); int x = getWIDth() / 2; int y = getHeight() / 2; if (false) { canvas.drawPicture(mPicture); } else { drawPict(canvas,x,y,1,1); drawPict(canvas,-1,-1); drawPict(canvas,-1); } } @OverrIDe public VIEwParent invalIDateChildInParent(int[] location,Rect dirty) { location[0] = getleft(); location[1] = gettop(); dirty.set(0,getWIDth(),getHeight()); return getParent(); } @OverrIDe protected voID onLayout(boolean changed,int l,int t,int r,int b) { final int count = super.getChildCount(); for (int i = 0; i < count; i++) { final VIEw child = getChildAt(i); if (child.getVisibility() != GONE) { final int childleft = getpaddingleft(); final int childtop = getpaddingtop(); child.layout(childleft,childtop,childleft + child.getMeasureDWIDth(),childtop + child.getMeasuredHeight()); } } }}解决方法
//little changes in this pIEce of code float[] dst; //Globalpublic SampleVIEw(Context context) { super(context); setFocusable(true); mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.image1); float w = mBitmap.getWIDth(); float h = mBitmap.getHeight(); // construct our mesh 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; setXY(mVerts,fy); setXY(mOrig,fy); index += 1; dst=mVerts;//Assign dst here just once } } mMatrix.setTranslate(10,10); mMatrix.invert(mInverse); } @OverrIDe protected voID onDraw(Canvas canvas) { canvas.drawcolor(0xFFCCCCCC); canvas.concat(mMatrix); canvas.drawBitmapMesh(mBitmap,null); } private voID warp(float cx,float cy) { final float K = 10000; float[] src = dst; //Now you are applying wrap effect on the last effected pixels 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; float d = floatMath.sqrt(dd); float pull = K / (dd + 0.000001f); pull /= (d + 0.000001f); // androID.util.Log.d("skia","index " + i + " dist=" + d + // " pull=" + pull); if (pull >= 1) { dst[i + 0] = cx; dst[i + 1] = cy; } else { dst[i + 0] = x + dx * pull; dst[i + 1] = y + dy * pull; } } }总结
以上是内存溢出为你收集整理的Android – 图像扭曲效果全部内容,希望文章能够帮你解决Android – 图像扭曲效果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)