編輯:Android 開發實例
ImageSwitcher 是 Android 中用來控制圖片展示效果的一個元件,例如幻燈片式的切換效果,頗有質感,拿來做相簿可說是一絕。
源碼下載
setImageURI(Uri uri):設定圖片位址
setImageResource(int resid):設定圖片資源庫
setImageDrawable(Drawable drawable):繪製圖片
- <ImageSwitcher android:id="@+id/switcher"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_alignParentTop="true"
- android:layout_alignParentLeft="true"
- />
- <Gallery android:id="@+id/gallery"
- android:background="#55000000"
- android:layout_width="match_parent"
- android:layout_height="60dp"
- android:layout_alignParentBottom="true"
- android:layout_alignParentLeft="true"
- android:gravity="center_vertical"
- android:spacing="16dp"
- />
is = (ImageSwitcher) findViewById(R.id.switcher);
is.setFactory(this);
設定動畫效果
is.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
is.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
1. 布局檔
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <ImageSwitcher android:id="@+id/switcher"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_alignParentTop="true"
- android:layout_alignParentLeft="true"
- />
- <Gallery android:id="@+id/gallery"
- android:background="#55000000"
- android:layout_width="match_parent"
- android:layout_height="60dp"
- android:layout_alignParentBottom="true"
- android:layout_alignParentLeft="true"
- android:gravity="center_vertical"
- android:spacing="16dp"
- />
- </RelativeLayout>
2. Java 程式碼
- package wjq.WidgetDemo;
- import android.app.Activity;
- import android.content.Context;
- import android.os.Bundle;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.Window;
- import android.view.animation.AnimationUtils;
- import android.widget.AdapterView;
- import android.widget.BaseAdapter;
- import android.widget.Gallery;
- import android.widget.ImageSwitcher;
- import android.widget.ImageView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.AdapterView.OnItemSelectedListener;
- import android.widget.Gallery.LayoutParams;
- import android.widget.ViewSwitcher.ViewFactory;
- public class ImageSwitcherDemo extends Activity implements
- OnItemSelectedListener, ViewFactory {
- private ImageSwitcher is;
- private Gallery gallery;
- private Integer[] mThumbIds = { R.drawable.b, R.drawable.c,
- R.drawable.d, R.drawable.f, R.drawable.g,
- };
- private Integer[] mImageIds = { R.drawable.b, R.drawable.c,
- R.drawable.d, R.drawable.f, R.drawable.g, };
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.imageswitcherpage);
- is = (ImageSwitcher) findViewById(R.id.switcher);
- is.setFactory(this);
- is.setInAnimation(AnimationUtils.loadAnimation(this,
- android.R.anim.fade_in));
- is.setOutAnimation(AnimationUtils.loadAnimation(this,
- android.R.anim.fade_out));
- gallery = (Gallery) findViewById(R.id.gallery);
- gallery.setAdapter(new ImageAdapter(this));
- gallery.setOnItemSelectedListener(this);
- }
- @Override
- public View makeView() {
- ImageView i = new ImageView(this);
- i.setBackgroundColor(0xFF000000);
- i.setScaleType(ImageView.ScaleType.FIT_CENTER);
- i.setLayoutParams(new ImageSwitcher.LayoutParams(
- LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
- return i;
- }
- public class ImageAdapter extends BaseAdapter {
- public ImageAdapter(Context c) {
- mContext = c;
- }
- public int getCount() {
- return mThumbIds.length;
- }
- public Object getItem(int position) {
- return position;
- }
- public long getItemId(int position) {
- return position;
- }
- public View getView(int position, View convertView, ViewGroup parent) {
- ImageView i = new ImageView(mContext);
- i.setImageResource(mThumbIds[position]);
- i.setAdjustViewBounds(true);
- i.setLayoutParams(new Gallery.LayoutParams(
- LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
- i.setBackgroundResource(R.drawable.e);
- return i;
- }
- private Context mContext;
- }
- @Override
- public void onItemSelected(AdapterView<?> parent, View view, int position,
- long id) {
- is.setImageResource(mImageIds[position]);
- }
- @Override
- public void onNothingSelected(AdapterView<?> parent) {
- }
- }
轉自:https://www.cnblogs.com/salam/archive/2010/10/06/1844660.html
Android 進度提示:用 ProgressBar 做一個轉不停的小圈圈
當 App 要下載檔案、讀取資料庫或等待網路回應時,使用者需要一個「它還在動」的訊號,否則很容易以為程式當掉了。Android 的 ProgressBar 就是為這件事而
Android 視圖控件架構解析:View 與 ViewGroup
控件樹的組成 Android 的視圖控件大致分為兩類:ViewGroup 與 View。ViewGroup 充當父容器,負責包含並管理子 View,兩者交錯組合便形成一棵
Android ListView 加載狀態設計與 ViewStub 技巧
ListView 加載提示可用 Dialog、Toast 或內嵌提示。較常見的是把提示放在列表上方,或用 ViewStub 覆蓋內容。ViewStub 的 inflate
Android 進度提示元件:Spinner 與 ProgressBar 的實作方式
在 Android 上,若要向使用者傳達「工作進行中」,最直接的方式就是顯示進度條。ProgressBar 有兩種外觀:可標示百分比的長條式進度條,以及旋轉式的 spin