Android enables picture reversal, flip, rotate, zoom in and out _android

Source: Internet
Author: User
Tags image flip

**********************************************************************

Android to flip a picture

**********************************************************************

Resources res = This.getcontext (). Getresources ();
 IMG = Bitmapfactory.decoderesource (res, R.DRAWABLE.AA);
 Matrix matrix = new Matrix ();
 Matrix.postrotate (180); /* Flip 180 degrees *
 /int width = img.getwidth ();
 int height = img.getheight ();
 IMG_A = Bitmap.createbitmap (img, 0, 0, width, height, matrix, true);

The img_a can then be draw directly onto the canvas, Canvas.drawbitmap (Img_a, ten, p);

The Matrix is an important class for processing image effects such as flip, Zoom, and Matrix.postscale to set the zoom ratio, which defaults to 1

**********************************************************************
Android to achieve image rotation

**********************************************************************

public class Ex04_22 extends activity{private ImageView mimageview, private Button btn1,btn2, private TextView MTEXTVI
ew
Private Absolutelayout layout1;
 private int scaletimes=1,scaleangle=1;
 @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
 Setcontentview (R.layout.main);
 mimageview= (ImageView) Findviewbyid (R.id.myimageview);
 Final Bitmap Bmp=bitmapfactory.decoderesource (This.getresources (), r.drawable.ex04_22_1);
 Final int widthorig=bmp.getwidth ();
 Final int heightorig=bmp.getheight ();
 Mimageview.setimagebitmap (BMP);
 btn1= (Button) Findviewbyid (R.id.mybutton1);
 Btn1.setonclicklistener (New Onclicklistener () {public void OnClick (View v) {scaleangle--;
 if (scaleangle<-60) {scaleangle=-60;
 int newwidth=widthorig*scaletimes;
 int newheight=heightorig*scaletimes;
 Float scalewidth= ((float) newwidth)/widthorig;
 Float scaleheight= ((float) newheight)/heightorig;
 Matrix matrix=new Matrix (); Matrix.postscale (ScaleWidth, Scaleheight);
 Matrix.setrotate (5*scaleangle);
 Bitmap resizebitmap=bitmap.createbitmap (BMP, 0, 0, Widthorig, Heightorig, Matrix, True);
 Bitmapdrawable mynewbitmapdrawable=new bitmapdrawable (RESIZEBITMAP);
 Mimageview.setimagedrawable (mynewbitmapdrawable);
 }
 });
 Btn2= (Button) Findviewbyid (R.id.mybutton2);
 Btn2.setonclicklistener (New Onclicklistener () {public void OnClick (View v) {scaleangle++;
 if (scaleangle>60) {scaleangle=60;
 int newwidth=widthorig*scaletimes;
 int newheight=heightorig*scaletimes;
 Float scalewidth= ((float) newwidth)/widthorig;
 Float scaleheight= ((float) newheight)/heightorig;
 Matrix matrix=new Matrix ();
 Matrix.postscale (ScaleWidth, ScaleHeight);
 Matrix.setrotate (5*scaleangle);
 Bitmap resizebitmap=bitmap.createbitmap (BMP, 0, 0, Widthorig, Heightorig, Matrix, True);
 Bitmapdrawable mynewbitmapdrawable=new bitmapdrawable (RESIZEBITMAP);
 Mimageview.setimagedrawable (mynewbitmapdrawable);
 }
 }); }

**********************************************************************

To achieve the fading effect of the screen can be used: Setalpha (alpha);
Alpha from 255, gradually diminishing!

**********************************************************************

How to achieve the scrolling effect of the screen, there are two key points, one is to implement the Ongesturelistener, so that when the touch event occurs, the callback. Including press, scrolling, and so on, according to the API document, need two steps to achieve detection gesture behavior.

1) Create Gesturedetector instance

2 the Gesturedetector ontouchevent () method is invoked in the Ontouchevent () method.

Another key point is to implement a simple view to draw the picture.

The code looks like this. Since we do not need to use the layout definition, we do not need to provide an XML file.
Directly inside the program Setcontentview () can be.

Package Com.j2medev;
Import android.app.Activity;
Import Android.content.Context;
Import android.content.res.Resources;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
Import Android.graphics.Canvas;
Import Android.graphics.Paint;
Import Android.os.Bundle;
Import Android.view.GestureDetector;
Import android.view.MotionEvent;
Import Android.view.View;
Import Android.view.ViewGroup;
 
Import Android.view.GestureDetector.OnGestureListener; public class Horizontalscroll extends activity implements Ongesturelistener {private static final int x_max =; privat
e static final int y_max = 600;
private int scrollx = 0;
 
private int scrolly = 0;
MyView main;
Bitmap bmp;
Bitmap adapt;
Resources Res;
Paint Paint;
 
Gesturedetector Gesturescanner;
 
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Gesturescanner = new Gesturedetector (this);
 
Paint = new paint ();
res = getresources (); BMP = Bitmapfactory.decoderesource (res, R.DRAWABLE.ARC);
 
adapt = Bitmap.createbitmap (BMP);
main = new MyView (this);
Setcontentview (Main, New Viewgroup.layoutparams (800, 600));
 
@Override public boolean ontouchevent (motionevent me) {return gesturescanner.ontouchevent (me);} public boolean onscroll (Motionevent E1, motionevent E2, float Distancex, float distancey) {main.handlescroll (Distancex,
Distancey);
return true;
 
public boolean Ondown (Motionevent e) {return true;}
 
public boolean onfling (Motionevent E1, motionevent E2, float Velocityx, float velocityy) {return true;} public void Onlongpress (Motionevent e) {} public void onshowpress (Motionevent e) {} public boolean onsingletapup (Mot
 
Ionevent e) {return true;} /////////////////////////////////////class MyView extends View {public MyView
 
) {super (context);}
 
@Override protected void OnDraw (Canvas Canvas) {Canvas.drawbitmap (adapt,-SCROLLX,-scrolly, paint); public void Handlescroll (float disTX, float disty) {//X-axis////////////////////////////////if (Distx > 6.0) {if (Scrollx < 460) {SCROLLX
 + 15;
 } else if (Distx < -6.0) {if (Scrollx >=) {scrollx = 15; 
 }//////////////////////////////////////////////Y-axis//////////////////////////////////if (DistY > 6.0) {
 if (Scrolly <) {scrolly = 15;
 } else if (Disty < -6.0) {if (scrolly >=) {scrolly = 15; }////////////////////////////////////////////////if (Scrollx <=) && (scrolly <= 120)) {//
 adapt = Bitmap.createbitmap (BMP, Scrollx, scrolly, 320, 480);
 Invalidate ();
} invalidate (); }
}
}

**********************************************************************

Teach you to work with images on Google Android platform

**********************************************************************

manipulating image pixels

Now you can handle the individual pixels. By using the Getpixels in the Android.graphics.Bitmap API, you can load pixels into an array of integers.

In this example, you will color each pixel according to certain rules. After this processing, all pixels will be converted to a byte code ranging from 0 to 255.

The setpixels in the Android.graphics.Bitmap API is used to load this integer array into an image.

The final step is to update the screen with the ImageView variable MIV. Here is a snippet of code to implement this dyeing process.

private void tintthepicture (int deg) {int[] pix = new INT[PICW * Pich];
 
 Mbitmap.getpixels (pix, 0, PICW, 0, 0, PICW, Pich);
 int RY, GY, by, Ryy, Gyy, ByY, R, G, B, Y;
 Double angle = (3.14159d * (double) deg)/180.0d;
 int S = (int) (256.0D * Math.sin (angle));
 
 int C = (int) (256.0D * Math.Cos (angle));
 for (int y = 0; y < Pich. y++) for (int x = 0; x < PICW x + +) {int index = y * PICW + x;
 int r = (Pix[index] >>) & 0xFF;
 int g = (Pix[index] >> 8) & 0xFF;
 int b = Pix[index] & 0xff;
 RY = (* r-59 * g-11 * b)/100;
 GY = ( -30 * r + g-11 * b)/100;
 by = ( -30 * r-59 * g + * b)/100;
 Y = (r + * g + A * b)/100;
 Ryy = (S * by + C * RY)/256;
 BYY = (C * by-s * RY)/256;
 Gyy = ( -51 * RYY-19 * byy)/100;
 R = Y + ryy; R = (r < 0)?
 0: ((R > 255) 255:r);
 G = Y + gyy; g = (g < 0)?
 0: ((G > 255) 255:g);
 B = Y + byy; b = (b < 0)?
 0: ((B > 255) 255:b); Pix[index] = 0xff000000 | (R << 16) | (G << 8) |
 B
 Bitmap BM = Bitmap.createbitmap (PICW, Pich, false);
 
 Bm.setpixels (pix, 0, PICW, 0, 0, PICW, Pich);
 Put the updated bitmap into the main view miv.setimagebitmap (BM);
 
 Miv.invalidate ();
 Mbitmap = BM;
 pix = NULL; }

**********************************************************************

Zooming in and out of Android pictures

**********************************************************************

public class Ex04_22 extends activity{private ImageView mimageview, private Button btn1,btn2, private TextView Mtextview
;
Private Absolutelayout layout1;
Private Bitmap bmp;
private int id=0;
private int displaywidth,displayheight;
private float scalewidth=1,scaleheight=1;
Private final static String filename= "/data/data/ex04_22.lcs/ex04_22_2.png"; @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (
R.layout.main);
Get screen resolution Displaymetrics dm=new displaymetrics ();
Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);
Displaywidth=dm.widthpixels;
displayheight=dm.heightpixels-80;
Bmp=bitmapfactory.decoderesource (This.getresources (), r.drawable.ex04_22_1);
layout1= (absolutelayout) Findviewbyid (R.ID.LAYOUT1);
mimageview= (ImageView) Findviewbyid (R.id.myimageview);
btn1= (Button) Findviewbyid (R.id.mybutton1);
Btn1.setonclicklistener (New Onclicklistener () {public void OnClick (View v) {small ();}}); Btn2= (Button) Findviewbyid (r.id.mybutTon2);
Btn2.setonclicklistener (New Onclicklistener () {public void OnClick (View v) {big ();}}); } private void Small () {//Get bitmap high and wide int bmpwidth=bmp.getwidth (); int bmpheight=bmp.getheight ();//Set Zoom out double scale=
0.8;
Calculate the proportion to be reduced scalewidth= (float) (Scalewidth*scale);
scaleheight= (float) (Scaleheight*scale);
Matrix Matrix=new Matrix () of the bitmap object after producing resize;
Matrix.postscale (ScaleWidth, ScaleHeight);
Bitmap resizebmp=bitmap.createbitmap (BMP, 0, 0, bmpwidth, Bmpheight, Matrix, True);
if (id==0) {Layout1.removeview (Mimageview);} else{Layout1.removeview ((ImageView) Findviewbyid (ID);} id++;
ImageView imageview=new ImageView (this);
Imageview.setid (ID);
Imageview.setimagebitmap (resizebmp);
Layout1.addview (ImageView);
Setcontentview (LAYOUT1);
Btn2.setenabled (TRUE); private void Big () {//Get bitmap high and wide int bmpwidth=bmp.getwidth (); int bmpheight=bmp.getheight ();//Set Zoom out double scale=1.
25;
Calculate the proportion to be reduced scalewidth= (float) (Scalewidth*scale);
scaleheight= (float) (Scaleheight*scale); Produce ResizeAfter the bitmap object matrix Matrix=new matrix ();
Matrix.postscale (ScaleWidth, ScaleHeight);
Bitmap resizebmp=bitmap.createbitmap (BMP, 0, 0, bmpwidth, Bmpheight, Matrix, True);
if (id==0) {Layout1.removeview (Mimageview);} else{Layout1.removeview ((ImageView) Findviewbyid (ID);} id++;
ImageView imageview=new ImageView (this);
Imageview.setid (ID);
Imageview.setimagebitmap (resizebmp);
Layout1.addview (ImageView);
Setcontentview (LAYOUT1); if (scalewidth*scale*bmpwidth>displaywidth| |
Scaleheight*scale*scaleheight>displayheight) {btn2.setenabled (false);}} }

XML file

<?xml version= "1.0" encoding= "Utf-8"?> <absolutelayout android:id=
"@+id/layout1"
android: Layout_width= "Fill_parent"
android:layout_height= "fill_parent"
xmlns:android= "http://" Schemas.android.com/apk/res/android "
>
<imageview
android:id=" @+id/myimageview "
Android : layout_width= "200px"
 
android:layout_height= "150px"
android:src= "@drawable/ex04_22_1"
android: layout_x= "0px"
android:layout_y= "0px"
>
</ImageView>
<button
android:id= " @+id/mybutton1 "
 
android:layout_width=" 90px "
android:layout_height=" 60px "
android:text=" shrink "
android:textsize= "18SP"
android:layout_x= "20px"
android:layout_y= "372px"
 
>
</ button>
<button
android:id= "@+id/mybutton2"
android:layout_width= "90px"
android: layout_height= "60px"
android:text= "magnify"
android:textsize= "18sp"
 
android:layout_x= "210px"
Android:layout_y= "372px"
>
</Button>
</AbsoluteLayout>

*********************************************************************

Android Image Transparency processing code

*********************************************************************

public static Bitmap Setalpha (Bitmap sourceimg, int number) {
 
int[] argb = new Int[sourceimg.getwidth () * sourceimg.  GetHeight ()];
 
Sourceimg.getpixels (ARGB, 0, Sourceimg.getwidth (), 0, 0,sourceimg.getwidth (), Sourceimg.getheight ());//Get the ARGB value of the picture  Number
 
= number * 255/100;
 
for (int i = 0; i < argb.length i++) {
 
ARGB = (number << 24) | (ARGB & 0X00FFFFFF)//Modify the highest 2-bit value
 
}
 
sourceimg = Bitmap.createbitmap (ARGB, Sourceimg.getwidth (), SOURCEIMG.G  Etheight (), config.argb_8888);
 
return sourceimg;
 
}

This is all about Android image processing, including Android image reversal, Android image Flip, Android image rotation, fading out, zooming in and out of Android images, and teaching you to work with images on Google's Android platform.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.