Android应用开发控件——Gallery和ImageSwitcher
生活随笔
收集整理的這篇文章主要介紹了
Android应用开发控件——Gallery和ImageSwitcher
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
??? Gallery組件主要用于橫向顯示圖像列表,不過按常規(guī)做法。Gallery組件只能有限地顯示指定的圖像。也就是說,如果為Gallery組件指定了10張圖像,那么當(dāng)Gallery組件顯示到第10張時,就不會再繼續(xù)顯示了。這雖然在大多數(shù)時候沒有什么關(guān)系,但在某些情況下,我們希望圖像顯示到最后一張時再重第1張開始顯示,也就是循環(huán)顯示。要實(shí)現(xiàn)這種風(fēng)格的Gallery組件,就需要對Gallery的Adapter對象進(jìn)行一番改進(jìn)。
以下通過Gallery模擬循環(huán)顯示圖像,在單擊某一個Gallery組件中的圖像時在下方顯示一個放大的圖像(使用ImageSwitcher組件)。
最終效果圖如下:
一、Layout布局文件
A:主界面布局文件
View Code <?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">
<ImageButton android:id="@+id/ibtnHead"
android:layout_height="60px" android:layout_width="60px"
android:src="@drawable/icon" android:scaleType="fitXY"></ImageButton>
</LinearLayout>
B:顯示Gallery和ImageSwitcher的布局文件
View Code <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Gallery android:id="@+id/img_gallery" android:layout_width="fill_parent"
android:layout_height="110px" android:layout_marginTop="2px"
android:layout_alignParentLeft="true"></Gallery>
<ImageSwitcher android:id="@+id/img_switcher"
android:layout_width="90px" android:layout_height="90px"
android:layout_centerHorizontal="true" android:layout_below="@+id/img_gallery"
></ImageSwitcher>
</RelativeLayout>
C:String文件
View Code <?xml version="1.0" encoding="utf-8"?><resources>
<string name="addContact_PleaseChooseImg">請選擇圖像</string>
</resources>
二、設(shè)置圖片資源
在drawable-hdpi文件夾下放置10個要顯示的圖片,如下圖
三、主Activity類
View Code package cn.moon.contact;import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.ViewSwitcher.ViewFactory;
public class AddContactActivity extends Activity {
ImageButton ibtnImgChoose;
AlertDialog imgChooseDialog;
Gallery gallery;
ImageSwitcher imageSwitcher;
int selectedImage;
private int[] images = { R.drawable.png0001, R.drawable.png0002,
R.drawable.png0003, R.drawable.png0004, R.drawable.png0005,
R.drawable.png0006, R.drawable.png0007, R.drawable.png0008,
R.drawable.png0009, R.drawable.png0010, };
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addcontact);
System.out.println("AddContact");
ibtnImgChoose = (ImageButton) findViewById(R.id.ibtnHead);
ibtnImgChoose.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
initImageChoose();
imgChooseDialog.show();
}
});
}
private void initImageChoose() {
LayoutInflater layoutInflater = LayoutInflater.from(this);
View view = layoutInflater.inflate(R.layout.userheadchoose, null);
gallery = (Gallery) view.findViewById(R.id.img_gallery);
gallery.setAdapter(new imageAdapter(this));
gallery.setSelection(images.length / 2);
imageSwitcher = (ImageSwitcher) view.findViewById(R.id.img_switcher);
imageSwitcher.setFactory(new switcherFactory(this));
gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
imageSwitcher.setImageResource(images[arg2]);
selectedImage = arg2;
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("請選擇圖像");
builder.setPositiveButton("確認(rèn)", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ibtnImgChoose.setImageResource(images[selectedImage]);
}
});
builder.setNegativeButton("取消", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setView(view);
imgChooseDialog = builder.create();
}
class imageAdapter extends BaseAdapter {
private Context context;
public imageAdapter(Context context) {
super();
this.context = context;
}
public int getCount() {
return images.length;
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);// 構(gòu)造一個ImageView
imageView.setImageResource(images[position]);// 設(shè)置ImageView圖片源
imageView.setAdjustViewBounds(true);
imageView.setScaleType(ScaleType.FIT_XY);// 自適應(yīng)高和寬
imageView.setLayoutParams(new Gallery.LayoutParams(80, 80));// 設(shè)置顯示圖處的大小
imageView.setPadding(10, 5, 10, 5);// 設(shè)置四邊的距離
return imageView;
}
}
class switcherFactory implements ViewFactory {
private Context context;
public switcherFactory(Context context) {
super();
this.context = context;
}
public View makeView() {
ImageView imageView = new ImageView(context);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(90, 90));
return imageView;
}
}
}
轉(zhuǎn)載于:https://www.cnblogs.com/ycmoon/archive/2011/04/12/2013497.html
總結(jié)
以上是生活随笔為你收集整理的Android应用开发控件——Gallery和ImageSwitcher的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 32位单精度浮点乘法器的FPGA实现
- 下一篇: 【转】 看IT人才最容易犯的17个错误