Android中的缩略图制作
2016-03-09 21:44
507 查看
1、xml文件
2、制作缩略图
完工!!!
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="51dp" android:onClick="onClick" android:text="缩略图" /> <ImageView android:id="@+id/show_iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:src="@drawable/ic_launcher" /> </RelativeLayout>
2、制作缩略图
package com.phone.day24_imagebitmap; import java.io.ByteArrayOutputStream; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.ImageView; /** * 制作图片缩略图 * * * */ public class MainActivity extends Activity { ImageView show_tv; Bitmap resourseImage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); show_tv = (ImageView) findViewById(R.id.show_iv); // 将图片从资源文件中拿出 resourseImage = BitmapFactory.decodeResource(getResources(), R.drawable.image); // 显示当前资源图片 show_tv.setImageBitmap(resourseImage); } public void onClick(View view) { // TODU 制作缩略图 // 将Bitmap 转换为数组 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); resourseImage.compress(Bitmap.CompressFormat.JPEG, 100, outputStream); // 制作缩略图 Bitmap map = getBitmap(outputStream.toByteArray(), 200, 100); show_tv.setImageBitmap(map); } /** * 制作缩略图 * * * @param bytes * 数组资源 * @param newWidth * 新的宽度 * @param newheight * 新的高度 * @return */ public Bitmap getBitmap(byte[] bytes, int newWidth, int newheight) { // 可以来设置一些图片的属性参数 BitmapFactory.Options options = new BitmapFactory.Options(); // 紧解码图片边缘 设置为true 获取宽高(节省内存) , 但是 decodeByteArray 方法的返回值为null, options.inJustDecodeBounds = true; // 用options 属性 解码图片边缘 BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options); // 获取图片宽高 int oldWidth = options.outWidth; int oldHeight = options.outHeight; // 计算新的比例 int scalWidth = oldWidth / newWidth; int scalheight = oldHeight / newheight; // 计算缩放比例 int scal = scalWidth > scalheight ? scalheight : scalWidth; // 设置该方法 为false 否则 decodeByteArray 将返回为null options.inJustDecodeBounds = false; // 设置缩放比例 options.inSampleSize = scal; // 转换新图片 Bitmap map = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options); // 返回 return map; } }
完工!!!
相关文章推荐
- androidStudio如何更改SDK的版本
- Android-百度地图-定位功能
- 如何在android studio中实现自定义view
- android MediaPlayer理解_我的第一篇博客
- Android Studio 下使用自定义属性注意点
- Android -- Looper.prepare()和Looper.loop() —深入版
- Android内存分析
- Android新手入门2016(5)--HelloWorld工程结构分析
- Android--持久化技术
- Android--广播机制
- Android动画--Activity过渡动画 Transition
- Android--Fragment
- Android中Intent的各种常见作用
- 关于android studio中添加新的moudle实现自定义view无法引用的问题
- /Users/alamps/AndroidStudioProjects/Demo11ListView
- Android--Activity
- Android--入门
- 基于Android Webview的Hybrid App开发的前端优化
- mac os使用lsusb命令和连接未知的Android设备
- 隐藏式抽屉(SlidingDrawer)