The Android system supports operations such as pan, rotate, scale, and tilt on the Matrix object control graphics. You can also pan, rotate, and scale the View components.
Import android. content. context; import android. graphics. bitmap; import android. graphics. canvas; import android. graphics. matrix; import android. graphics. drawable. bitmapDrawable; import android. util. attributeSet; import android. view. keyEvent; import android. view. view; public class MyView extends View {// The initial image resource private Bitmap bitmap; // The Matrix instance private Matrix matrix = new Matrix (); // sets the inclination private float sx = 0.0f; // bitmap width and height private int width and height; // scaling ratio private float scale = 1.0f; // determine whether to scale or rotate private boolean isScale = false; public MyView (Context context, AttributeSet set) {super (context, set); // obtain bitmap = (BitmapDrawable) context. getResources (). getDrawable (R. drawable. a )). getBitmap (); // obtain the bitmap width = bitmap. getWidth (); // obtain the bitmap height = bitmap. getHeight (); // get the current view focus this. setFocusable (true);} protected Void onDraw (Canvas canvas) {super. onDraw (canvas); // reset Matrixmatrix. reset (); if (! IsScale) {// rotate Matrixmatrix. setSkew (sx, 0);} else {// scale Matrixmatrix. setScale (scale);} // create a new Bitmap bitmap2 = Bitmap image based on the original Bitmap and Matrix. createBitmap (bitmap, 0, 0, width, height, matrix, true); // draw a new bitmap canvas. drawBitmap (bitmap2, matrix, null);} public boolean onKeyDown (int keyCode, KeyEvent event) {switch (keyCode) {// press A to tilt case KeyEvent to the left. KEYCODE_A: isScale = false; sx + = 0.1; postInvalidate (); break; // press the key D to tilt the case KeyEvent to the right. KEYCODE_D: isScale = false; sx-= 0.1; postInvalidate (); break; // press the W button to enlarge case KeyEvent. KEYCODE_W: isScale = true; if (scale <2.0) scale + = 0.1; postInvalidate (); break; // press the S button to narrow down case KeyEvent. KEYCODE_S: isScale = true; if (scale> 0.5) scale-= 0.1; postInvalidate (); break;} return super. onKeyDown (keyCode, event );}}
Canvas provides a drawBitmapMesh method to distort Bitmap objects. The effect is equivalent to a filter in PS, which can be distorted, such as water ripple and Red Flag.
DrawBitmapMesh (Bitmap bitmap, int meshWidth, int meshHeight, float [] verts, int vertOffset, int [] colors, int colorOffset, Paint)
Here, bitmap is the source of the image to be distorted, and meshWidth and meshHeight are the number of cells that divide bitmap horizontally and vertically,
Verts is a length of (meshWidth + 1) * (meshHeight + 1)
VertOffset controls verts arrays to distort bitmap from the first few array elements
Import android. app. activity; import android. content. context; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. graphics. canvas; import android. graphics. color; import android. OS. bundle; import android. view. motionEvent; import android. view. view; public class WarpTest extends Activity {private Bitmap bitmap; public void onCreate (Bundle savedInstanceState) {super. onCreate (savedI NstanceState); setContentView (new MyView (this, R. drawable. jinta);} private class MyView extends View {// defines two constants, which specify that the image is divided into 20 cells both horizontally and vertically. Private final int WIDTH = 20; private final int HEIGHT = 20; // record that the image contains 441 vertices private final int COUNT = (WIDTH + 1) * (HEIGHT + 1); // defines an array and stores the coordinates of 21*21 points on Bitmap, private final float [] verts = new float [COUNT * 2]. // define an array to record the distorted coordinates of 21x21 points on Bitmap. // The Key to twisting an image is to modify the element values in the array. Private final float [] orig = new float [COUNT * 2]; public MyView (Context context, int drawableId) {super (context); setFocusable (true ); // load the image bitmap = BitmapFactory Based on the specified resource. decodeResource (getResources (), drawableId); // obtain the image width and height float bitmapWidth = bitmap. getWidth (); float bitmapHeight = 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; // initialize orig and verts arrays. After initialization, The orig and verts // arrays uniformly store the x of the 21x21 points, y coordinate orig [index * 2 + 0] = verts [index * 2 + 0] = fx; orig [index * 2 + 1] = verts [index * 2 + 1] = fy; index + = 1 ;}// set the background Color setBackgroundColor (Color. WHITE);} protected void onDraw (Canvas canvas) {// distort bitmap by verts array // distort canvas from the first vertex (controlled by 5th parameters 0. drawBitmapMesh (bitmap, WIDTH, HEIGHT, verts, 0, null, 0, null);} // tool method, calculate the private void warp (float cx, float cy) {for (int I = 0; I <COUNT * 2; I + = 2) {float dx = cx-orig [I + 0]; float dy = cy-orig [I + 1]; float dd = dx * dx + dy * dy; // calculates the float d = (float) Math distance between each coordinate point and the current point (cx, cy. sqrt (dd); // calculates the curvature, Which is farther away from the current point (cx, cy). The smaller the curvature, the float pull = 80000/(float) (dd * d )); // re-assign the value of if (pull> = 1) {verts [I + 0] = cx to the verts array (Save the coordinates of 21x21 vertices on bitmap; verts [I + 1] = cy ;} else {// control the point offset of each vertex to the touch event verts [I + 0] = orig [I + 0] + dx * pull; verts [I + 1] = orig [I + 1] + dy * pull;} // notify the View component to re-paint invalidate ();} public boolean onTouchEvent (MotionEvent event) {// call the warp method to distort the verts array warp (event. getX (), event. getY (); return true ;}}}
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + signature/expires/WwOA8L3A + signature + 0Ke5 + signature/expires + signature/k1si + 0Ke5 + signature + csignature vcD4KPHA + pgltzybzcm9" http://www.2cto.com/uploadfile/Collfiles/20140408/2014040809293434.jpg "alt =" \ ">
Import android. app. activity; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. graphics. bitmapShader; import android. graphics. color; import android. graphics. composeShader; import android. graphics. linearGradient; import android. graphics. porterDuff; import android. graphics. radialGradient; import android. graphics. shader; import android. graphics. sweepGradient; import android. graphics. shader. tileMode; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; public class ShaderTest extends Activityimplements OnClickListener {// declare the bitmap rendering object private Shader [] shaders = new Shader [5]; // declare the color array private int [] colors; myView myView; // custom View class public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); myView = (MyView) findViewById (R. id. my_view); // obtain Bitmap instance Bitmap bm = BitmapFactory. decodeResource (getResources (), R. drawable. water); // set the gradient Color group, that is, gradient colors = new int [] {Color. RED, Color. GREEN, Color. BLUE}; // instantiate BitmapShader, which repeats the image in the x direction, and shaders [0] = new BitmapShader (bm, TileMode. REPEAT, TileMode. MIRROR); // instantiate LinearGradientshaders [1] = new LinearGradient (0, 0,100,100, colors, null, TileMode. REPEAT); // instantiate RadialGradientshaders [2] = new RadialGradient (100,100, 80, colors, null, TileMode. REPEAT); // instantiate SweepGradientshaders [3] = new SweepGradient (160,160, colors, null); // instantiate ComposeShadershaders [4] = new ComposeShader (shaders [1], shaders [2], PorterDuff. mode. DARKEN); Button bn1 = (Button) findViewById (R. id. bn1); Button bn2 = (Button) findViewById (R. id. bn2); Button bn3 = (Button) findViewById (R. id. bn3); Button bn4 = (Button) findViewById (R. id. bn4); Button bn5 = (Button) findViewById (R. id. bn5); bn1.setOnClickListener (this); bn2.setOnClickListener (this); bn3.setOnClickListener (this); bn4.setOnClickListener (this); bn5.setOnClickListener (this);} public void onClick (View source) {switch (source. getId () {case R. id. bn1: myView. paint. setShader (shaders [0]); break; case R. id. bn2: myView. paint. setShader (shaders [1]); break; case R. id. bn3: myView. paint. setShader (shaders [2]); break; case R. id. bn4: myView. paint. setShader (shaders [3]); break; case R. id. bn5: myView. paint. setShader (shaders [4]); break;} // re-paint interface myView. invalidate ();}}