例如:
/res/drawable/androidbiancheng.png 的 ID 是 R.drawable.androidbiancheng
佈局文件, main.xml
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ImageView
android:id="@+id/myimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/myimagewidth"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/myimageheight"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
package com.AndroidBtmap;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class AndroidBtmap extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView myImage = (ImageView)findViewById(R.id.myimage);
TextView myImageWidth = (TextView)findViewById(R.id.myimagewidth);
TextView myImageHeight = (TextView)findViewById(R.id.myimageheight);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.androidbiancheng);
int bitmapWidth = bitmap.getWidth();
int bitmapHeight = bitmap.getHeight();
myImage.setImageBitmap(bitmap);
myImageWidth.setText("Width: " + String.valueOf(bitmapWidth));
myImageHeight.setText("Height: " + String.valueOf(bitmapHeight));
}
}
下一篇文章:
- 儲存位圖檔案
- 圖像處理
沒有留言:
發佈留言