在网上看了个例子,依着葫芦画瓢,先不谈与屏幕等宽,随便设了个高和宽,不知为何报空指针异常
public class Test extends Activity {
private Bitmap bmImg;
private ImageView imView;
private static final String tag = "Image Top";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bmImg = BitmapFactory.decodeResource(getResources(), R.drawable.top);
imView.setImageDrawable(resizeImage(bmImg, 200, 200)); }
public static Drawable resizeImage(Bitmap bitmap, int w, int h) {
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)); float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height; Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width,
height, matrix, true); return new BitmapDrawable(resizedBitmap); }
}
我确定图片是必然存在的,xml是默认的没改,不知错在哪了啊
public class Test extends Activity {
private Bitmap bmImg;
private ImageView imView;
private static final String tag = "Image Top";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bmImg = BitmapFactory.decodeResource(getResources(), R.drawable.top);
imView.setImageDrawable(resizeImage(bmImg, 200, 200)); }
public static Drawable resizeImage(Bitmap bitmap, int w, int h) {
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)); float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height; Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width,
height, matrix, true); return new BitmapDrawable(resizedBitmap); }
}
我确定图片是必然存在的,xml是默认的没改,不知错在哪了啊
解决方案 »
- android 底部菜单 求解
- 急求指导~~~
- .菜鸟求教数据保存问题
- 急急急:怎么设计一个回调方法来更新进度条??请求指点,谢谢!!!
- I3还是I5,我该如何选择?
- ? - Dev Tools的Show CPU Usage为什么选不了
- 请朋友来帮忙看看吧!按上下文菜单的载体反应!?
- android模拟器中,怎么查看后台运行的进程
- 为什么没有选项
- 【提问】做了一个安卓应用需要连局域网的,之前都是用自己电脑作服务器,能否把服务器放在网上 ?这样就不是连局域网了,要做怎样的改变呢?
- 求助:Eclipse3.5安装ADT居然会出现以下错误!!!!!!!
- 使用同样的Linux kernel在不同的手机版本的板子上跑,有的有USB设备,有的没有USB设备???
<?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/logo"
android:src="@drawable/top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
将R.drawable.top改为R.id.logo试试
imView = (ImageView)findViewById(R.id.logo);