Android中几种图片特效的处理的实现方法
程序员文章站
2024-02-18 14:10:22
android中几种图片特效的处理
这里有放大缩小图片,获得圆角图片,获得带倒影图片的几种方法及实现代码,
package com.android....
android中几种图片特效的处理
这里有放大缩小图片,获得圆角图片,获得带倒影图片的几种方法及实现代码,
package com.android.tutor; import android.graphics.bitmap; import android.graphics.canvas; import android.graphics.lineargradient; import android.graphics.matrix; import android.graphics.paint; import android.graphics.pixelformat; import android.graphics.porterduffxfermode; import android.graphics.rect; import android.graphics.rectf; import android.graphics.bitmap.config; import android.graphics.porterduff.mode; import android.graphics.shader.tilemode; import android.graphics.drawable.drawable; public class imageutil { //放大缩小图片 public static bitmap zoombitmap(bitmap bitmap,int w,int h){ int width = bitmap.getwidth(); int height = bitmap.getheight(); matrix matrix = new matrix(); float scalewidht = ((float)w / width); float scaleheight = ((float)h / height); matrix.postscale(scalewidht, scaleheight); bitmap newbmp = bitmap.createbitmap(bitmap, 0, 0, width, height, matrix, true); return newbmp; } //将drawable转化为bitmap public static bitmap drawabletobitmap(drawable drawable){ int width = drawable.getintrinsicwidth(); int height = drawable.getintrinsicheight(); bitmap bitmap = bitmap.createbitmap(width, height, drawable.getopacity() != pixelformat.opaque ? bitmap.config.argb_8888 : bitmap.config.rgb_565); canvas canvas = new canvas(bitmap); drawable.setbounds(0,0,width,height); drawable.draw(canvas); return bitmap; } //获得圆角图片的方法 public static bitmap getroundedcornerbitmap(bitmap bitmap,float roundpx){ bitmap output = bitmap.createbitmap(bitmap.getwidth(), bitmap .getheight(), config.argb_8888); canvas canvas = new canvas(output); final int color = 0xff424242; final paint paint = new paint(); final rect rect = new rect(0, 0, bitmap.getwidth(), bitmap.getheight()); final rectf rectf = new rectf(rect); paint.setantialias(true); canvas.drawargb(0, 0, 0, 0); paint.setcolor(color); canvas.drawroundrect(rectf, roundpx, roundpx, paint); paint.setxfermode(new porterduffxfermode(mode.src_in)); canvas.drawbitmap(bitmap, rect, rect, paint); return output; } //获得带倒影的图片方法 public static bitmap createreflectionimagewithorigin(bitmap bitmap){ final int reflectiongap = 4; int width = bitmap.getwidth(); int height = bitmap.getheight(); matrix matrix = new matrix(); matrix.prescale(1, -1); bitmap reflectionimage = bitmap.createbitmap(bitmap, 0, height/2, width, height/2, matrix, false); bitmap bitmapwithreflection = bitmap.createbitmap(width, (height + height/2), config.argb_8888); canvas canvas = new canvas(bitmapwithreflection); canvas.drawbitmap(bitmap, 0, 0, null); paint deafalutpaint = new paint(); canvas.drawrect(0, height,width,height + reflectiongap, deafalutpaint); canvas.drawbitmap(reflectionimage, 0, height + reflectiongap, null); paint paint = new paint(); lineargradient shader = new lineargradient(0, bitmap.getheight(), 0, bitmapwithreflection.getheight() + reflectiongap, 0x70ffffff, 0x00ffffff, tilemode.clamp); paint.setshader(shader); // set the transfer mode to be porter duff and destination in paint.setxfermode(new porterduffxfermode(mode.dst_in)); // draw a rectangle using the paint with our linear gradient canvas.drawrect(0, height, width, bitmapwithreflection.getheight() + reflectiongap, paint); return bitmapwithreflection; } }
修改main.xml布局文件,主要放了两个imageview控件,代码如下:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/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 com.android.tutor; import android.app.activity; import android.graphics.bitmap; import android.graphics.drawable.drawable; import android.os.bundle; import android.widget.imageview; public class imagedemo extends activity { private imageview mimageview01,mimageview02; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); setupviews(); } private void setupviews(){ mimageview01 = (imageview)findviewbyid(r.id.image01); mimageview02 = (imageview)findviewbyid(r.id.image02); //获取壁纸返回值是drawable drawable drawable = getwallpaper(); //将drawable转化为bitmap bitmap bitmap = imageutil.drawabletobitmap(drawable); //缩放图片 bitmap zoombitmap = imageutil.zoombitmap(bitmap, 100, 100); //获取圆角图片 bitmap roundbitmap = imageutil.getroundedcornerbitmap(zoombitmap, 10.0f); //获取倒影图片 bitmap reflectbitmap = imageutil.createreflectionimagewithorigin(zoombitmap); //这里可以让bitmap再转化为drawable // drawable rounddrawable = new bitmapdrawable(roundbitmap); // drawable reflectdrawable = new bitmapdrawable(reflectbitmap); // mimageview01.setbackgrounddrawable(rounddrawable); // mimageview02.setbackgrounddrawable(reflectdrawable); mimageview01.setimagebitmap(roundbitmap); mimageview02.setimagebitmap(reflectbitmap); } }
以上就是android中几种图片特效的方法整理,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
上一篇: Oracle11g使用sql
下一篇: Java线程的生命周期的详解
推荐阅读