在网上看了个例子,依着葫芦画瓢,先不谈与屏幕等宽,随便设了个高和宽,不知为何报空指针异常
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是默认的没改,不知错在哪了啊
解决方案 »
- 菜鸟做扫雷项目出现 java.lang.ArithmeticExce问题,求大虾指教
- 求android中如何关闭服务?
- 有谁知道这个控件的实现原理吗?
- 关于gallery的问题
- 【急】求助Android屏保的实现问题
- 关于一个UI的设计,仿中关村在线android客户端,请大家帮下忙。
- activity与activity之间跳转,finish方法可有可无?
- 分析log方法
- android在一个app中怎么关闭另一个开着的app
- 打算将一个大型的c++项目移植到安卓,考虑使用ndk生成.so文件,再在AndroidStudio里面调用
- 求助: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);