Hello everyone, this section is to share some of the Android image special effects processing tips, such as rounded corners, reflection, there is a picture scaling, drawable into bitmap,bitmap into drawable and so on.
Talk less, directly to explain today's example, this example is mainly to get wallpaper (Getwallpaper ()), and then some of the current wallpaper effects processing. Step by step:
The first step: a new Android project named Imagedemo, the engineering structure is as follows:
Step Two: Create a new one. Java file, named Imageutil.java, in which you define some image processing methods, the code is as follows:
Package com.android.tutor;
Import Androidgraphicsbitmap;
Import Androidgraphicscanvas;
Import androidgraphicslineargradient;
Import Androidgraphicsmatrix;
Import Androidgraphicspaint;
Import Androidgraphicspixelformat;
Import Androidgraphicsporterduffxfermode;
Import Androidgraphicsrect;
Import ANDROIDGRAPHICSRECTF;
Import Androidgraphicsbitmapconfig;
Import Androidgraphicsporterduffmode;
Import Androidgraphicsshadertilemode;
Import androidgraphicsdrawabledrawable; public class Imageutil {//Zoom out picture public static Bitmap Zoombitmap (Bitmap bitmap,int w,int h) {int width = b
Itmapgetwidth ();
int height = bitmapgetheight ();
Matrix matrix = new Matrix ();
float SCALEWIDHT = ((float) w/width);
float ScaleHeight = ((float) h/height);
Matrixpostscale (SCALEWIDHT, ScaleHeight);
Bitmap newbmp = Bitmapcreatebitmap (Bitmap, 0, 0, width, height, matrix, true);
return newbmp; //Convert drawable to Bitmap public static Bitmap DrawabletObitmap (drawable drawable) {int width = drawablegetintrinsicwidth ();
int height = drawablegetintrinsicheight (); Bitmap Bitmap = bitmapcreatebitmap (width, height, drawablegetopacity ()!= pixelformatopaque?
bitmapconfigargb_8888:bitmapconfigrgb_565);
Canvas Canvas = new Canvas (bitmap);
Drawablesetbounds (0,0,width,height);
Drawabledraw (canvas);
return bitmap; ///Get fillet picture method public static Bitmap Getroundedcornerbitmap (Bitmap bitmap,float roundpx) {Bitmap OUTP
UT = Bitmapcreatebitmap (Bitmapgetwidth (), Bitmap getheight (), configargb_8888);
Canvas Canvas = new Canvas (output);
final int color = 0xff424242;
Final Paint Paint = new Paint ();
Final Rect Rect = new Rect (0, 0, bitmapgetwidth (), Bitmapgetheight ());
Final RECTF RECTF = new RECTF (rect);
Paintsetantialias (TRUE);
Canvasdrawargb (0, 0, 0, 0);
Paintsetcolor (color); CanvasDrawroundrect (RECTF, ROUNDPX, ROUNDPX, paint);
Paintsetxfermode (New Porterduffxfermode (modesrc_in));
Canvasdrawbitmap (Bitmap, rect, rect, paint);
return output; //Get a picture with reflection method public static Bitmap Createreflectionimagewithorigin (Bitmap Bitmap) {final int reflectiongap = 4
;
int width = bitmapgetwidth ();
int height = bitmapgetheight ();
Matrix matrix = new Matrix ();
Matrixprescale (1,-1);
Bitmap reflectionimage = Bitmapcreatebitmap (Bitmap, 0, HEIGHT/2, width, HEIGHT/2, matrix, false);
Bitmap bitmapwithreflection = bitmapcreatebitmap (width, height + height/2), configargb_8888);
Canvas Canvas = new Canvas (bitmapwithreflection);
Canvasdrawbitmap (bitmap, 0, 0, NULL);
Paint deafalutpaint = new Paint ();
Canvasdrawrect (0, Height,width,height + reflectiongap, deafalutpaint);
Canvasdrawbitmap (reflectionimage, 0, height + reflectiongap, null); PaintPaint = new paint (); LinearGradient shader = new LinearGradient (0, Bitmapgetheight (), 0, Bitmapwithreflectiongetheight () + RE
Flectiongap, 0x70ffffff, 0X00FFFFFF, Tilemodeclamp);
Paintsetshader (shader);
Set the Transfer mode to is Porter Duff and destination in Paintsetxfermode (new Porterduffxfermode (modedst_in)); Draw a rectangle using the paint with our linear gradient canvasdrawrect (0, height, width, bitmapwithreflecti
Ongetheight () + reflectiongap, paint);
return bitmapwithreflection;
}
}
Step three: Modify the Main.xml layout file, mainly put two ImageView controls, the code is as follows:
<?xml version= "0" encoding= "utf-8"?> <linearlayout xmlns:android=
"http://schemasandroidcom/apk/res/ Android "
android:orientation=" vertical "
android:layout_width=" fill_parent "
android:layout_height=" Fill_parent "
>
<imageview
android:id=" @+id/image01 "
android:layout_width=" Wrap_content "
android:layout_height= "wrap_content"
android:padding= "10px"
/>
<imageview
Android:id= "@+id/image02"
android:layout_width= "wrap_content"
android:layout_height= "Wrap_content"
android:padding= "10px"
/>
</LinearLayout>
Fourth step: Modify the main core program, Imagedemo.java, the code is as follows:
Package comandroidtutor;
Import androidappactivity;
Import Androidgraphicsbitmap;
Import androidgraphicsdrawabledrawable;
Import Androidosbundle;
Import Androidwidgetimageview;
public class Imagedemo extends activity {private ImageView mimageview01,mimageview02;
public void OnCreate (Bundle savedinstancestate) {superoncreate (savedinstancestate);
Setcontentview (Rlayoutmain);
Setupviews ();
private void Setupviews () {mImageView01 = (ImageView) Findviewbyid (RIDIMAGE01);
mImageView02 = (ImageView) Findviewbyid (RIDIMAGE02);
Get wallpaper return value is drawable drawable drawable = Getwallpaper ();
Convert drawable to Bitmap Bitmap Bitmap = Imageutildrawabletobitmap (drawable);
Zoom picture Bitmap Zoombitmap = Imageutilzoombitmap (Bitmap, 100, 100);
Get fillet picture Bitmap Roundbitmap = Imageutilgetroundedcornerbitmap (Zoombitmap, 0f);
Get the reflection picture Bitmap Reflectbitmap = Imageutilcreatereflectionimagewithorigin (Zoombitmap); //This allows bitmap to be converted to drawable//drawable rounddrawable = new bitmapdrawable (ROUNDBITMAP);
drawable reflectdrawable = new bitmapdrawable (REFLECTBITMAP);
Mimageviewsetbackgrounddrawable (rounddrawable);
Mimageviewsetbackgrounddrawable (reflectdrawable);
Mimageviewsetimagebitmap (ROUNDBITMAP);
Mimageviewsetimagebitmap (REFLECTBITMAP); }
}
Fifth step: Run the above project, view the effect as follows:
OK, it's done!!
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.