当前位置:首页 » 其他

圆角,倒影,圆形图片,截图,获取图片资源

2016-03-31 19:55 本站整理 浏览(10)

首先是布局:

<?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="100dp"

android:layout_height="100dp"

android:padding="10px"

/>

<ImageView

android:id="@+id/image02"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:padding="10px"

/>

<ImageView

android:id="@+id/image03"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:padding="10px"

/>

</LinearLayout>

然后是工具类:

package com.example.guangbo;

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;

}

public static Bitmap createCircleImage(Bitmap source, int min)

{

final Paint paint = new Paint();

paint.setAntiAlias(true);

Bitmap target = Bitmap.createBitmap(min, min, Config.ARGB_8888);

//产生一个同样大小的画布

Canvas canvas = new Canvas(target);

// 首先绘制圆形

canvas.drawCircle(min / 2, min / 2, min / 2, paint);

// 使用SRC_IN

paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));

// 绘制图片

canvas.drawBitmap(source, 0, 0, paint);

return target;

}

}

最后在activity中实现:

public class YuanActivity extends Activity {

private ImageView mImageView01,mImageView02,mImageView03;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_yuan);

super.onCreate(savedInstanceState);

setupViews();

}

private void setupViews(){

mImageView01 = (ImageView)findViewById(R.id.image01);

mImageView02 = (ImageView)findViewById(R.id.image02);

mImageView03 = (ImageView)findViewById(R.id.image03);

//Resources res=getResources();

//Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.init_pic);

//bmp=ImageUtil.getRoundedCornerBitmap(bmp, 100.0f);

//Bitmap bb=ImageUtil.drawableToBitmap(tupiantwo.getDrawable());

//mImageView01.setImageBitmap(bmp);

//获取壁纸返回值是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 yuan=ImageUtil.createCircleImage(zoomBitmap,60);

//这里可以让Bitmap再转化为Drawable

// Drawable roundDrawable = new BitmapDrawable(roundBitmap);

// Drawable reflectDrawable = new BitmapDrawable(reflectBitmap);

// mImageView01.setBackgroundDrawable(roundDrawable);

// mImageView02.setBackgroundDrawable(reflectDrawable);

mImageView01.setImageBitmap(roundBitmap);

mImageView02.setImageBitmap(reflectBitmap);

mImageView03.setImageBitmap(yuan);

}

}

/////////////////////////////////////////////////////分离线///////////////////////////////////////////////////////////////////////

截图的实现(可以截整个屏幕),也可以直接一小块,拿一个RelativeLayout 做例子:

private RelativeLayout jt; //声明

jt=(RelativeLayout) findViewById(R.id.jt); //找到

Bitmap btm=getScreenShot(jt); //传入RelativeLayout

然后:

//获取截图

private Bitmap getScreenShot(RelativeLayout waterPhoto) {

// View是你需要截图的View

View view = waterPhoto;

view.setDrawingCacheEnabled(true);

view.buildDrawingCache();

Bitmap b1 = view.getDrawingCache();

/*// 获取状态栏高度

Rect frame = new Rect();

activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);

int statusBarHeight = frame.top;

System.out.println(statusBarHeight);*/

/*// 获取屏幕长和高

int width = activity.getWindowManager().getDefaultDisplay().getWidth();

int height = activity.getWindowManager().getDefaultDisplay()

.getHeight();*/

//获取长和高

int width=view.getWidth();

int height=view.getHeight();

// 去掉标题栏

// Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);

Bitmap b = Bitmap.createBitmap(b1, 0, 0, width, height);

view.destroyDrawingCache();

return b;

}

截图搞定

//////////////////////////////////////////////////////////////////华丽分界线/////////////////////////////////////////////////////////////////////////////////////////////////////

获取drawable下的图片:

Resources res=getResources();

Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.init_pic);

从imageView中获取bitmap:

imageview.setDrawingCacheEnabled(true); //设置可以获取

imageview.getDrawingCache(); //获取

imageview.setDrawingCacheEnabled(false); //清空缓存

//////////////数据转换///////////////////////////

//Bitmap----->byte[]

public byte[] Bitmap2Bytes(Bitmap bm) {

ByteArrayOutputStream baos = new ByteArrayOutputStream();

bm.compress(Bitmap.CompressFormat.PNG, 100, baos);

return baos.toByteArray();

}

//byte[]------->Bitmap

public Bitmap Bytes2Bimap(byte[] b) {

if (b.length != 0) {

return BitmapFactory.decodeByteArray(b, 0, b.length);

} else {

return null;

}

}