欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android高手进阶教程(二十二)之Android中几种图像特效处理的集锦汇总!!

程序员文章站 2024-03-01 13:55:52
大家好,这一节给大家分享的是android中几种图像特效处理的小技巧,比如圆角,倒影,还有就是图片缩放,drawable转化为bitmap,bitmap转化为drawabl...

大家好,这一节给大家分享的是android中几种图像特效处理的小技巧,比如圆角,倒影,还有就是图片缩放,drawable转化为bitmap,bitmap转化为drawable等等.

废话少说了,直接讲解今天的实例,本例主要是先获取壁纸(getwallpaper()),然后对当前壁纸的一些特效处理.大家按步骤一步一步来:

第一步:新建一个android工程命名为imagedemo,工程结构如下:Android高手进阶教程(二十二)之Android中几种图像特效处理的集锦汇总!!

第二步:新建一个.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, 0, 0, width, height, matrix, true); 
    return newbmp; 
  } 
  //将drawable转化为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; 
       
    } 
    
   //获得圆角图片的方法 
  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, 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; 
  } 
  //获得带倒影的图片方法 
  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); 
     
    paint paint = new paint(); 
    lineargradient shader = new lineargradient(0, 
        bitmapgetheight(), 0, 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, height, width, bitmapwithreflectiongetheight() 
        + reflectiongap, 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); 
  } 
    
     
} 

第五步:运行上述工程,查看效果如下:
Android高手进阶教程(二十二)之Android中几种图像特效处理的集锦汇总!!
ok大功告成了!!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。