学习了一下压缩和水印,以后要用到的时候可以直接来这里拷贝
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.bitmap_compress.MainActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="300sp"
android:contentDescription="@null"
android:src="@drawable/a" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
MainActivity.Java
package com.example.bitmap_compress;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView imageView;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//图片压缩
imageView = (ImageView) findViewById(R.id.imageView1);
Bitmap bitmap = BitmapCompressTools.decodeSampledBitmapFromResource(
getResources(), R.drawable.a, 100, 100);
Log.i("Original bytes",
"----->>>"
+ BitmapFactory.decodeResource(getResources(),
R.drawable.a).getByteCount());
Log.i("Compressed bytes", "----->>>" + bitmap.getByteCount());
imageView.setImageBitmap(bitmap);
//水印,其实就是往图片上写字
button = (Button) this.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bitmap bitmap = BitmapTools.createBitmap(getResources(),
R.drawable.a, "a.png");
imageView.setImageBitmap(bitmap);
}
});
}
}
然后给出每个的工具类
BitmapTools.java
package com.example.bitmap_compress;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Environment;
public class BitmapTools {
public static Bitmap createBitmap(Resources resources, int resid, String name) {
Bitmap bitmap = BitmapFactory.decodeResource(resources, resid);
// 复制一份新的Bitmap,因为不能直接在原有的bitmap上进行水印操作
// Bitmap.config存储的格式
Bitmap newBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
// 使用自定义画布
Canvas canvas = new Canvas(newBitmap);
Paint paint = new Paint();
paint.setTextSize(200);
canvas.drawText("hello", 100, 100, paint);
// 判断SDcard是否在可用状态
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
// 直接将图片保存在根目录下
File root = Environment.getExternalStorageDirectory();
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(new File(root, name));
// 对图片进行压缩并以png格式,保存在sdcard中
newBitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
} catch (Exception e) {
}
}
return newBitmap;
}
}
BitmapCompressTools.java
package com.example.bitmap_compress;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class BitmapCompressTools {
public static Bitmap decodeSampledBitmapFromResource(Resources resources,
int resId, int width, int height) {
// 给定的BitmapFactory设置解码的参数
BitmapFactory.Options options = new BitmapFactory.Options();
// 从解码器中获得原始图片的宽高,而避免申请内存空间
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(resources, resId, options);
options.inSampleSize = calculateInSampleSize(options, width, height);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(resources, resId, options);
}
/**
* 指定输出图片的缩放比例
* @param options
* @param reqWidth
* @param reqHeight
* @return
*/
public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// 获得原始图片的宽高
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
int inSimpleSize = 1;
if (imageHeight > reqHeight || imageWidth > reqWidth) {
// 计算压缩的比例:分为宽高比例
final int heightRatio = Math.round((float) imageHeight
/ (float) reqHeight);
final int widthRatio = Math.round((float) imageWidth
/ (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will
// guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSimpleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSimpleSize;
}
}