设为首页 加入收藏

TOP

Android图片放大修改代码(一)
2014-11-24 03:00:53 来源: 作者: 【 】 浏览:3
Tags:Android 图片 放大 修改 代码

package com.jinyan.TestImage;


import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


public class TestImage extends Activity {
/** Called when the activity is first created. */


private static final String tag = "TestImage.............";

String imageUrl = "https://www.cppentry.com/upload_files/article/54/1_bz7tj__.png";
Bitmap bmImg;
ImageView imView;


Button button1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imView = (ImageView) findViewById(R.id.imview);
imView.setImageBitmap(returnBitMap(imageUrl));


Bitmap bitmap = returnBitMap(imageUrl);


imView.setImageDrawable(resizeImage(bitmap, 600, 600));
}

public Bitmap returnBitMap(String url) {
URL myFileUrl = null;
Bitmap bitmap = null;
try {
myFileUrl = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
Log.v(tag,bitmap.toString());

return bitmap;
}


public static Drawable resizeImage(Bitmap bitmap, int w, int h) {


// load the origial Bitmap
Bitmap BitmapOrg = bitmap;


int width = BitmapOrg.getWidth();
int height = BitmapOrg.getHeight();
int newWidth = w;
int newHeight = h;

Log.v(tag,String.valueOf(width));
Log.v(tag,String.valueOf(height));

Log.v(tag,String.valueOf(newWidth));
Log.v(tag,String.valueOf(newHeight));


// calculate the scale
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;


// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the Bitmap
matrix.postScale(scaleWidth, scaleHeight);
// if you want to rotate the Bitmap
// matrix.postRotate(45);


// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width,
height, matrix, true);


// make a Drawable from Bitmap to allow to set the Bitmap
// to the ImageView, ImageButton or what ever
return new BitmapDrawable(resizedBitmap);


}

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇关于Android如何获取屏幕分辨率的.. 下一篇Android中电池信息(Battery infor..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Sphinx : 高性能SQL (2025-12-24 10:18:11)
·Pandas 性能优化 - (2025-12-24 10:18:08)
·MySQL 索引 - 菜鸟教 (2025-12-24 10:18:06)
·Shell 基本运算符 - (2025-12-24 09:52:56)
·Shell 函数 | 菜鸟教 (2025-12-24 09:52:54)