2012年8月18日 星期六
android -- ImageView
Matrix - 放大和縮小
public class Cq_Matrix01Activity extends Activity{
/** Called when the activity is first created. */
private int bmpWidth;
private int bmpHeight;
private Bitmap bitmap;
//private LinearLayout linearLayout;
private ImageView mImageView;
private Button btnSmall;
private Button btnReset;
private Button btnLarge;
private float scale;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mImageView = (ImageView)findViewById(R.id.imageView1);
btnLarge = (Button)findViewById(R.id.btnEnlarge);
btnReset = (Button)findViewById(R.id.reset);
btnSmall = (Button)findViewById(R.id.btnShrink);
//透過 BitmapFactory.decodeResource 取得 bitmap
bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.yih911030_01_300);
//取得寬高
bmpWidth = bitmap.getWidth();
bmpHeight = bitmap.getHeight();
scale = 1;
loadImage();
//設定放大按鈕監聽器
btnLarge.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
scale *= 1.3;
loadImage();
}
});
//設定原來大小監聽器
btnReset.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
scale = 1;
loadImage();
}
});
//設定縮小按鈕監聽器
btnSmall.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
scale *= 0.7;
loadImage();
}
});
}
private void loadImage(){
android.graphics.Matrix matrix = new android.graphics.Matrix();
matrix.postScale(scale, scale);
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bmpWidth, bmpHeight, matrix, true);
mImageView.setImageBitmap(resizedBitmap);
}
}
***********************************
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout01"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="*** 圖示放大縮小 ***"
android:layout_gravity="center" />
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" >
<Button
android:id="@+id/btnEnlarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="21sp"
android:text="放大" />
<Button
android:id="@+id/reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17sp"
android:text="原尺寸" />
<Button
android:id="@+id/btnShrink"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="縮小"
/>
</LinearLayout>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center"
android:layout_gravity="center"
/>
</LinearLayout>
2012年5月17日 星期四
Drawable Image 02
利用 xml 來定義圖檔
繪圖子類別(Drawable) xml 檔案內的標籤名稱
AnimationDrawable <animation-list>
BitmapDrawable <bitmap>
ClipDrawable <clip>
ColorDrawable <color>
GradientDrawable <shape>
InsetDrawable <onset>
LayerDrawable <layer-list>
LevelListDrawable <level-list>
RotateDrawable <rotate>
ScaleDrawable <scale>
StateListDrawable <selector>
TransitionDrawable <transition>
e.g.
res/drawable/transition_example.xml
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/icon01"/>
<item android:drawable="@drawable/icon02/>
</transition>
Main.java (主程式):
public class ImageTransitionActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//設定轉換圖像來自 res/drawable/transition_example.xml
Resources res = getResources();
TransitionDrawable transiton =
(TransitionDrawable)res.getDrawable(R.drawable.transition_example);//取得xml檔
ImageView mImageView = (ImageView)findViewById(R.id.imageView1);
mImageView.setImageDrawable(transiton); //把取得的drawable圖像放到mImageView上
//開始轉換到第2張圖像
transiton.startTransition(5000);
}
}
Drawable Image
要繪製簡單的圖形,可以使用Drawable類別的抽象物件,它會被擴展出來成為不同的具體Drawable 圖形,包括有BitmapDrawable, ShapeDrawable, PictureDrawable, LayerDrawable,當然也可以有自己定義的 Drawable.
三種方法可以定義和實做一個Drawable物件:
1、使用圖像,將之存在專案的資源(res/drawable);
2、使用xml檔案來定義Drawable屬性;
3、直接寫碼。
最簡單的方式是將圖檔直接放入應用程式,然後由專案資源來獲取圖像檔案。可以使用的格式有.png, jpg, gif.
第一種方法:(ImageView 取圖像檔時用 .setImageResource(R.drawable.icon) )
先將 icon.png 存在 res/drawable目錄下
public class Cq_Img01Activity extends Activity {
/** Called when the activity is first created. */
LinearLayout mLinearLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Create a LinearLayout for ImageView 建立畫面佈局
mLinearLayout = new LinearLayout(this);
//Create ImageView obj 實做圖像顯示欄位,然後從res/drawable目錄獲取圖像,放入自身
ImageView imageview = new ImageView(this);
imageview.setImageResource(R.drawable.icon);
//Set ImageView
imageview.setAdjustViewBounds(true);
imageview.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mLinearLayout.addView(imageview);
setContentView(mLinearLayout);
}
}
*******************
要操作圖像本身時(譬如以手指移動圖像),首先得建立一個Drawable 類別的圖像資源物件。自圖像資源(Resources)取得圖像資源的ID(R.drawable.icon),定義成圖像物件,才能使用。
Resources res = mContext.getResources();
Drawable myDrawableObj = res.getDrawable(R.drawable.icon);
******************************************************************
第二種方法:2、使用xml檔案來定義Drawable屬性
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/icon" />
</LinearLayout>
public class ImageActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView (R.layout.main);
}
}
*****************************************************
三種方法可以定義和實做一個Drawable物件:
1、使用圖像,將之存在專案的資源(res/drawable);
2、使用xml檔案來定義Drawable屬性;
3、直接寫碼。
最簡單的方式是將圖檔直接放入應用程式,然後由專案資源來獲取圖像檔案。可以使用的格式有.png, jpg, gif.
第一種方法:(ImageView 取圖像檔時用 .setImageResource(R.drawable.icon) )
先將 icon.png 存在 res/drawable目錄下
public class Cq_Img01Activity extends Activity {
/** Called when the activity is first created. */
LinearLayout mLinearLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Create a LinearLayout for ImageView 建立畫面佈局
mLinearLayout = new LinearLayout(this);
//Create ImageView obj 實做圖像顯示欄位,然後從res/drawable目錄獲取圖像,放入自身
ImageView imageview = new ImageView(this);
imageview.setImageResource(R.drawable.icon);
//Set ImageView
imageview.setAdjustViewBounds(true);
imageview.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mLinearLayout.addView(imageview);
setContentView(mLinearLayout);
}
}
*******************
要操作圖像本身時(譬如以手指移動圖像),首先得建立一個Drawable 類別的圖像資源物件。自圖像資源(Resources)取得圖像資源的ID(R.drawable.icon),定義成圖像物件,才能使用。
Resources res = mContext.getResources();
Drawable myDrawableObj = res.getDrawable(R.drawable.icon);
******************************************************************
第二種方法:2、使用xml檔案來定義Drawable屬性
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/icon" />
</LinearLayout>
public class ImageActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView (R.layout.main);
}
}
*****************************************************
2012年3月27日 星期二
Image Transition
從一張圖,經過5秒轉換成另一張圖
res/layout/main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center"
android:src="@drawable/yih950909_01_300" />
</LinearLayout>
**************
res/drawable/kids_photos.xml:
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/yih950909_01_300"/>
<item android:drawable="@drawable/usa_03y_01_300"/>
</transition>
****************************
main.java:
public class Cq_ImgTransitionActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//設定轉換圖像來自 res/drawable/kids_photos.xml
Resources res = getResources();
TransitionDrawable tranisiton = (TransitionDrawable)res.getDrawable(R.drawable.kids_photos);
ImageView mImageView = (ImageView)findViewById(R.id.imageView1);
mImageView.setImageDrawable(tranisiton);
//開始轉換到第2張圖像
tranisiton.startTransition(5000);
}
}
res/layout/main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center"
android:src="@drawable/yih950909_01_300" />
</LinearLayout>
**************
res/drawable/kids_photos.xml:
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/yih950909_01_300"/>
<item android:drawable="@drawable/usa_03y_01_300"/>
</transition>
****************************
main.java:
public class Cq_ImgTransitionActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//設定轉換圖像來自 res/drawable/kids_photos.xml
Resources res = getResources();
TransitionDrawable tranisiton = (TransitionDrawable)res.getDrawable(R.drawable.kids_photos);
ImageView mImageView = (ImageView)findViewById(R.id.imageView1);
mImageView.setImageDrawable(tranisiton);
//開始轉換到第2張圖像
tranisiton.startTransition(5000);
}
}
ImageView
ImageView Programmatically
public class Cq_Img01Activity extends Activity {
/** Called when the activity is first created. */
LinearLayout mLinearLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Create a LinearLayout for ImageView
mLinearLayout = new LinearLayout(this);
//Create ImageView obj
ImageView imageview = new ImageView(this);
imageview.setImageResource(R.drawable.imgexample);
//Set size
imageview.setAdjustViewBounds(true);
imageview.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mLinearLayout.addView(imageview);
setContentView(mLinearLayout);
}
}
public class Cq_Img01Activity extends Activity {
/** Called when the activity is first created. */
LinearLayout mLinearLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Create a LinearLayout for ImageView
mLinearLayout = new LinearLayout(this);
//Create ImageView obj
ImageView imageview = new ImageView(this);
imageview.setImageResource(R.drawable.imgexample);
//Set size
imageview.setAdjustViewBounds(true);
imageview.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mLinearLayout.addView(imageview);
setContentView(mLinearLayout);
}
}
2012年3月18日 星期日
android -- image (Matrix)
******************************************
縮小圖:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
view = (ImageView) findViewById(R.id.imageView1);
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.butterfly);
bmpWidth = bitmap.getWidth();
bmpHeight = bitmap.getHeight();
scaleOriginal = 1;
loadImg();
}
private void loadImg(){
Matrix matrix = new Matrix();
scaleOriginal *= 0.5;
matrix.postScale(scaleOriginal, scaleOriginal);
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bmpWidth, bmpHeight, matrix, true);
view.setImageBitmap(resizedBitmap);
}
**********************************************************************
DisplayMetrics dm = new DisplayMetrics();
//取得手機解析度
getWindowManager().getDefaultDisplay().getMetrics(dm);
//設定顯示寬高
dispalyWidth = dm.widthPixels;
displayHeight = dm.heightPixels - 80;
//linearLayout = (LinearLayout)findViewById(R.id.linearLayout01);
*******************************************************************
DisplayMetrics displayMetrics = new DisplayMetrics();
((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(displayMetrics);
int statusBarHeight;
switch (displayMetrics.densityDpi) {
case DisplayMetrics.DENSITY_HIGH:
statusBarHeight = HIGH_DPI_STATUS_BAR_HEIGHT;
break;
case DisplayMetrics.DENSITY_MEDIUM:
statusBarHeight = MEDIUM_DPI_STATUS_BAR_HEIGHT;
break;
case DisplayMetrics.DENSITY_LOW:
statusBarHeight = LOW_DPI_STATUS_BAR_HEIGHT;
break;
default:
statusBarHeight = MEDIUM_DPI_STATUS_BAR_HEIGHT;
}
*************************************************
//縮放圖片
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
//重新縮放 Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bmpWidth, bmpHeight, matrix, true);
訂閱:
文章 (Atom)