Android 图像处理(类型转换,比例缩放,倒影,圆角)的小例子_Android

1.放大缩小图片

复制代码 代码如下:

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

2.获得圆角图片的方法

复制代码 代码如下:

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

3.获得带倒影的图片方法

复制代码 代码如下:

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

4.将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;
}

时间: 2024-11-30 22:03:59

Android 图像处理(类型转换,比例缩放,倒影,圆角)的小例子_Android的相关文章

Android 图像处理(类型转换,比例缩放,倒影,圆角)的小例子

1.放大缩小图片 复制代码 代码如下: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 / wi

Android调用系统的发邮件功能的小例子_Android

复制代码 代码如下: package com.example.myapi.email; import java.util.ArrayList;import java.util.List; import android.content.Context;import android.content.Intent;import android.net.Uri; public class SendEmailUtils {    private SendEmailUtils(){}    private

Android用ListView显示SDCard文件列表的小例子_Android

复制代码 代码如下: filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/ADASiteMaps/SigRecord";        File file=new File(filePath);        File[] files = file.listFiles(); 构造Adapter, 复制代码 代码如下:         for(File mCurrentFile:files)

android listview 水平滚动和垂直滚动的小例子_Android

网上有很多解决 android listview 水平和垂直滚动的代码,我没有按照他们说的做(以前没搜到 O(∩_∩)O~) 我采用的是添加HorizontalScrollViewJava代码 复制代码 代码如下:  < ScrollView android:id="@+id/ScrollView01" android:layout_height="300px" android:layout_x="16px" android:layout_

Android 取得状态栏、任务栏高度的小例子_Android

复制代码 代码如下: package com.aslibra.test; import android.app.Activity; import android.graphics.Rect; import android.os.Bundle; import android.util.Log; import android.view.Window; import android.widget.ImageView; public class test extends Activity {   Ima

android中sqlite的按条件查找的小例子_Android

name      hobby Tom        football Peter       basketball 我们如果要查找爱好是football的一行数据怎么办呢?在网上找了很多方法,感觉都说不到点子上,所以浪费了很多时间,好在终于解决了,解决方法如下 [java] 复制代码 代码如下: Cursor mcursor=mSQLiteDatabase.query(tableName, new String[]{"id as _id","name","

Android获取屏幕方向及键盘状态的小例子_Android

复制代码 代码如下: Configuration config = getResources().getConfiguration(); if (config.orientation == Configuration.ORIENTATION_LANDSCAPE){ //横屏,比如 480x320 }else if(config.orientation == Configuration.ORIENTATION_PORTRAIT){ //竖屏 ,标准模式 320x480 }else if(confi

Android图片特效:黑白特效、圆角效果、高斯模糊_Android

1.黑白效果 复制代码 代码如下: /**     * 将彩色图转换为黑白图     *      * @param 位图     * @return 返回转换好的位图     */    public static Bitmap convertToBlackWhite(Bitmap bmp) {        int width = bmp.getWidth(); // 获取位图的宽        int height = bmp.getHeight(); // 获取位图的高        

android,不显示标题的方法小例子_Android

在Value中的Style.xml中,添加:   复制代码 代码如下: <style name="NoTitle" >       <item name="android:windowNoTitle">true</item></style> 然后在androidManifest.xml中加入:android:theme="@style/NoTitle" 即可实现