大家好,这一节给大家分享的是AndroID中几种图像特效处理的小技巧,比如圆角,倒影,还有就是图片缩放,Drawable转化为Bitmap,Bitmap转化为Drawable等等.
废话少说了,直接讲解今天的实例,本例主要是先获取壁纸(getWallpaper()),然后对当前壁纸的一些特效处理.大家按步骤一步一步来:
第一步:新建一个AndroID工程命名为ImageDemo,工程结构如下:
第二步:新建一个.Java文件,命名为ImageUtil.java,在里面定义一些图片处理方法,代码如下:
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 { //放大缩小图片 public static Bitmap zoomBitmap(Bitmap bitmap,int w,int h){ int wIDth = bitmapgetWIDth(); int height = bitmapgetHeight(); Matrix matrix = new Matrix(); float scaleWIDht = ((float)w / wIDth); float scaleHeight = ((float)h / height); matrixpostscale(scaleWIDht,scaleHeight); Bitmap newbmp = BitmapcreateBitmap(bitmap,wIDth,height,matrix,true); return newbmp; } //将Drawable转化为Bitmap public static Bitmap drawabletoBitmap(Drawable drawable){ int wIDth = drawablegetIntrinsicWIDth(); int height = drawablegetIntrinsicHeight(); Bitmap bitmap = BitmapcreateBitmap(wIDth,drawablegetopacity() != PixelFormatopAQUE ? BitmapConfigARGB_8888 : BitmapConfigRGB_565); Canvas canvas = new Canvas(bitmap); drawablesetBounds(0,height); drawabledraw(canvas); return bitmap; } //获得圆角图片的方法 public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,float roundPx){ Bitmap output = 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,bitmapgetWIDth(),bitmapgetHeight()); final RectF rectF = new RectF(rect); paintsetAntiAlias(true); canvasdrawARGB(0,0); paintsetcolor(color); canvasdrawRoundRect(rectF,roundPx,paint); paintsetXfermode(new PorterDuffXfermode(ModeSRC_IN)); canvasdrawBitmap(bitmap,rect,paint); return output; } //获得带倒影的图片方法 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,height/2,false); Bitmap bitmapWithreflection = BitmapcreateBitmap(wIDth,(height + height/2),ConfigARGB_8888); Canvas canvas = new Canvas(bitmapWithreflection); canvasdrawBitmap(bitmap,null); Paint deafalutPaint = new Paint(); canvasdrawRect(0,height + reflectionGap,deafalutPaint); canvasdrawBitmap(reflectionImage,null); Paint paint = new Paint(); linearGradIEnt shader = new linearGradIEnt(0,bitmapgetHeight(),bitmapWithreflectiongetHeight() + reflectionGap,0x70ffffff,0x00ffffff,TileModeCLAMP); paintsetShader(shader); // Set the Transfer mode to be porter duff and destination in paintsetXfermode(new PorterDuffXfermode(ModeDST_IN)); // Draw a rectangle using the paint with our linear gradIEnt canvasdrawRect(0,paint); return bitmapWithreflection; } }
第三步:修改main.xml布局文件,主要放了两个ImageVIEw控件,代码如下:
<?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>
第四步:修改主核心程序,ImageDemo.java,代码如下:
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); //获取壁纸返回值是Drawable Drawable drawable = getWallpaper(); //将Drawable转化为Bitmap Bitmap bitmap = ImageUtildrawabletoBitmap(drawable); //缩放图片 Bitmap zoomBitmap = ImageUtilzoomBitmap(bitmap,100,100); //获取圆角图片 Bitmap roundBitmap = ImageUtilgetRoundedCornerBitmap(zoomBitmap,0f); //获取倒影图片 Bitmap reflectBitmap = ImageUtilcreateReflectionImageWithOrigin(zoomBitmap); //这里可以让Bitmap再转化为Drawable // Drawable roundDrawable = new BitmapDrawable(roundBitmap); // Drawable reflectDrawable = new BitmapDrawable(reflectBitmap); // mImageVIEwsetBackgroundDrawable(roundDrawable); // mImageVIEwsetBackgroundDrawable(reflectDrawable); mImageVIEwsetimageBitmap(roundBitmap); mImageVIEwsetimageBitmap(reflectBitmap); } }
第五步:运行上述工程,查看效果如下:
OK大功告成了!!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android高手进阶教程(二十二)之Android中几种图像特效处理的集锦汇总!!全部内容,希望文章能够帮你解决Android高手进阶教程(二十二)之Android中几种图像特效处理的集锦汇总!!所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)