`
zhujianjia
  • 浏览: 486528 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

转:Android中几种图像特效处理方法小结

 
阅读更多

原文地址:http://www.androidmi.com/Androidkaifa/jinjie/201011/1788.html

作者的联系方式:

QQ:524509159 
Email:hoohbood@gmail.com 
Tel:18666294230

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

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

第一步:新建一个Android工程命名为ImageDemo,工程结构如下:


第二步:新建一个.java文件,命名为ImageUtil.java,在里面定义一些图片处理方法,代码如下:

 

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>  
<?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);   
    }   
}  

 

第五步:运行上述工程,查看效果如下:


OK大功告成了!!

 

  • 大小: 24.4 KB
  • 大小: 18.5 KB
分享到:
评论

相关推荐

    android中图片色彩特效处理Demo

    在Android开发中,图片色彩特效处理是一个非常重要的领域,它涉及到图像处理算法和Android系统提供的图像API。这个"android中图片色彩特效处理Demo"旨在帮助开发者理解如何改变图片的色调、饱和度和亮度,从而创建出...

    安卓Android源码——常用图片特效处理.rar

    在安卓(Android)平台上,开发图像处理应用时,经常会涉及到各种图片特效的实现。这个压缩包"安卓Android源码——常用图片特效处理.rar"显然包含了用于处理图片特效的源代码,可以帮助开发者理解和学习如何在...

    Android 常用图片特效处理源码.zip

    首先,我们要明白Android中的图像处理涉及到的主要技术领域包括位图操作、色彩空间转换、滤镜应用以及图形渲染等。这些技术通常用于创建如模糊、旋转、裁剪、缩放、色彩调整、滤镜特效等常见功能。 1. **位图操作**...

    Android图片特效处理Demo

    这个“Android图片特效处理Demo”旨在提供一系列预设的图像处理效果,包括负片、锐化、旋转、浅色、光照、鱼眼和纹理等。接下来,我们将深入探讨这些特效以及实现它们的技术和工具。 1. **负片效果**:这是一种常见...

    android常用图片特效处理_Android.rar

    在Android开发中,图片特效处理是一项重要的技能,它涉及到用户界面的设计、用户体验的提升以及应用程序的性能优化。这个“android常用图片特效处理_Android.rar”压缩包可能包含了多个与Android平台图片处理相关的...

    Android 图像连环画特效源码.zip

    在Android平台上,图像处理和特效的应用是开发过程中一个有趣且具有挑战性的部分。"Android 图像连环画特效源码.zip"是一个专为Android开发者设计的资源,它提供了实现图像连环画特效的源代码。这个压缩包包含了实现...

    Android源码——图像连环画特效源码_new_38.zip

    研究这个源码,开发者可以学习如何在Android中实现复杂的图像处理特效,并将其应用到自己的项目中。 总的来说,"Android源码——图像连环画特效源码_new_38.zip"是一个宝贵的教育资源,它涵盖了Android图像处理的...

    Android的移动应用图像冰冻特效源码.rar

    冰冻特效在移动应用中通常涉及到帧动画和实时图像处理,这两个主题在Android开发中占据了重要位置。帧动画是指通过连续播放一系列静态图片来创建动态效果,而实时图像处理则涉及到对摄像头捕获或存储在设备中的图像...

    Android 图像冰冻特效源码.rar

    在Android平台上,图像处理技术是移动应用开发中的一个重要组成部分,特别是在游戏、图像编辑或视觉效果相关的应用程序中。"Android 图像冰冻特效源码.rar"是一个压缩包,包含了一个实现特定图像冰冻特效的源代码...

    Android中轴旋转特效实现,制作别样的图片浏览器

    在Android开发中,创建独特的用户体验往往需要利用各种视觉特效,其中一种吸引人的特效就是图片的中轴旋转。本文将深入探讨如何在Android中实现轴旋转特效,以制作一个别样的图片浏览器。 首先,我们需要理解“中轴...

    Android的移动应用图像柔化美白特效源码.rar

    在Android平台上,开发移动应用图像处理功能是一项常见的任务,尤其是对于照片编辑类应用而言,图像柔化和美白特效是必不可少的。本知识点将深入探讨如何使用Android SDK和相关的开源库来实现这一效果。 首先,我们...

    Android 图像连环画特效源码.rar

    在Android开发中,图像处理和特效的实现是一个重要的部分,特别是在创建吸引用户的UI或游戏时。这个"Android图像连环画特效源码"提供了一种将普通图片转换为具有连环画风格效果的方法,这对于创建独特的用户体验或者...

    Android代码-图像连环画特效源码.zip

    首先,连环画特效通常涉及到图像处理技术,这在Android中可以通过使用Java或者Kotlin语言,配合Android的图像库,如Bitmap和Canvas来实现。Bitmap是Android中的基本图像处理类,它可以加载、创建和操作图像。Canvas...

    Android100种图片处理效果大全项目

    《Android100种图片处理效果大全项目》是一款专注于Android平台上的图像处理示例集合,提供了丰富的图片操作功能。这个项目旨在帮助开发者深入理解和实践Android系统中的图像处理技术,包括但不限于滤镜效果、裁剪、...

    Android 加载数据等待特效

    在Android开发中,为了提升用户体验,尤其是在处理大量数据或者网络请求时,我们常常会使用“加载数据等待特效”(Loading Effects)来展示一个过渡状态,让用户知道应用正在后台忙碌并即将呈现所需内容。这种特效...

    android手势处理图片平移、缩放和旋转

    在Android开发中,手势识别是实现用户交互的关键技术之一,特别是在图像处理方面。"android手势处理图片平移、缩放和旋转"这个主题涉及到的主要知识点包括Android手势检测、ImageView的扩展以及图片操作。 首先,...

    一些非常炫的Android特效源码

    9. **实时滤镜和图像处理**:在图片应用或相机功能中,实时滤镜和图像处理特效是不可或缺的。源码可能涉及到OpenGL ES或renderscript的使用,实现图片的实时美颜、滤镜应用等功能。 10. **加载和空状态提示**:在...

    android图片处理集合demo

    使用OpenGL ES或RenderScript,可以将图像处理任务转移到GPU,提高处理速度,尤其适用于复杂的图像特效。 10. 图片选择器: 开发中,经常需要实现图片选择功能,可以集成像Android-Universal-Image-Loader这样的...

    Android JNI图像处理经典例子

    接着,在对应的C/C++代码中,需要定义这个方法,读取Bitmap数据,执行图像处理操作,最后更新Bitmap内容。 在IBMPhotoPhun这个示例中,可能包含了实现这些图像处理操作的源代码文件,如`imageprocessing.cpp`和相应...

Global site tag (gtag.js) - Google Analytics