ホームページ  >  記事  >  バックエンド開発  >  Android UI コントロール シリーズ: ギャラリー (ギャラリー ビュー)

Android UI コントロール シリーズ: ギャラリー (ギャラリー ビュー)

黄舟
黄舟オリジナル
2017-01-19 09:56:241512ブラウズ

ギャラリーはコンテンツを水平に表示でき、通常は写真を閲覧するために使用されます。選択されたオプションは中央にあり、イベントに応じて情報が表示されます。 ImageSwitcher コンポーネントを組み合わせて、サムネイルで画像を閲覧するプログラムを実装しましょう。具体的な手順は次のとおりです。

ステップ 1:

Andorid プロジェクト「GalleryTest」を作成します。プロジェクトのエントリ ポイントは、GalleryTest を継承するアクティビティ クラスです。アクティビティと OnItemSelectedListener および ViewFactory インターフェイスを実装して、画像とビューの作成を実現します

package org.hualang.Gallery;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ViewSwitcher.ViewFactory;
//继承Activity,实现onItemSelectedListener和ViewFactory接口
public class GalleryTest extends Activity implements OnItemSelectedListener,ViewFactory{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

        @Override
        public View makeView() {
                // TODO Auto-generated method stub
                return null;
        }

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {
                // TODO Auto-generated method stub

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

        }
}

ステップ 2:

プロジェクトの再描画可能なディレクトリに 7 枚の画像と対応するサムネイルを追加します

ステップ 3:

再レイアウトでレイアウト ファイルを作成しますプロジェクト main.xml のディレクトリに、Gallery コンポーネントと ImageSwitcher コンポーネントを追加し、対応するプロパティを設定します

<?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"  
    >  
<ImageSwitcher android:id="@+id/switcher"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"  
        android:layout_alignParentTop="true"  
        android:layout_alignParentLeft="true"  
    />  
  
    <Gallery android:id="@+id/gallery"  
        android:background="#55000000"  
        android:layout_width="fill_parent"  
        android:layout_height="60dp"  
        android:layout_alignParentBottom="true"  
        android:layout_alignParentLeft="true"  
        android:gravity="center_vertical"  
        android:spacing="16dp"  
    />  
</LinearLayout>

ステップ 4: 使用する ImageSwitcher インスタンスの画像リソースを GalleryTest の先頭で整数配列を宣言します

public class GalleryTest extends Activity implements OnItemSelectedListener,ViewFactory{
    /** Called when the activity is first created. */
        //声明ImageSwitcher
        private ImageSwitcher switcher;
        //缩略图片id数组
        private Integer[] thumbids={
                        R.drawable.thumb0,
                        R.drawable.thumb1,
                        R.drawable.thumb2,
                        R.drawable.thumb3,
                        R.drawable.thumb4,
                        R.drawable.thumb5,
                        R.drawable.thumb6,
                        R.drawable.thumb7
        };
        //图片id数组
        private Integer[] imgids={
                        R.drawable.img0,
                        R.drawable.img1,
                        R.drawable.img2,
                        R.drawable.img3,
                        R.drawable.img4,
                        R.drawable.img5,
                        R.drawable.img6,
                        R.drawable.img7
        };

ステップ 5 :

GalleryTest の onCreate() メソッドで、ウィンドウ スタイルを無題に設定し、現在のレイアウト ビューを設定し、ImageSwitcher インスタンスを取得し、フェードアウト アニメーションを設定して Gallery インスタンスを取得します

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置窗口特征无标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        //通过findViewById方法获得ImageSwitcher对象
        switcher=(ImageSwitcher)findViewById(R.id.switcher);
        //为ImageSwitcher设置工厂
        switcher.setFactory(this);
        //设置动画渐入效果
        switcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
        //设置动画渐出效果
        switcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
        //通过findViewById方法获得Gallery对象
        Gallery g=(Gallery)findViewById(R.id.gallery);
    }

ステップ 6:

内部クラス ImageAdapter を作成します。このクラスは BaseAdapter を継承し、ギャラリーのアダプター インスタンスを設定します

public class ImageAdapter extends BaseAdapter {
            //构造方法
                public ImageAdapter(Context c) {
                        mContext = c;
                }
                //获得数量
                public int getCount() {
                        return thumbids.length;
                }
                //获得当前选项
                public Object getItem(int position) {
                        return position;
                }
                //获得当前选项ID
                public long getItemId(int position) {
                        return position;
                }
                //获得View对象
                public View getView(int position, View convertView, ViewGroup parent) {
                        //实例化ImageView对象
                        ImageView i = new ImageView(mContext);
                        //设置缩略图片资源
                        i.setImageResource(thumbids[position]);
                        //设置边界对齐
                        i.setAdjustViewBounds(true);
                        //设置布局参数
                        i.setLayoutParams(new Gallery.LayoutParams(
                                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                        //设置背景资源
                        i.setBackgroundResource(R.drawable.picturefrom);
                        return i;
                }
                private Context mContext;
        }

ステップ 7:

onItemSelected() メソッドを実装し、画像を変更します

@Override  
        public void onItemSelected(AdapterView<?> adapter, View v, int position,  
                        long id) {  
                switcher.setImageResource(imgids[position]);  
        }

ステップ 8:

makeView() メソッドを実装しますImageView のレイアウト形式を設定します

@Override  
        public View makeView() {  
                // TODO Auto-generated method stub  
                //创建ImageView  
                ImageView i=new ImageView(this);  
                //设置背景颜色  
                i.setBackgroundColor(0xFF000000);  
                //设置精度类型  
                i.setScaleType(ImageView.ScaleType.FIT_CENTER);  
                //设置布局参数  
                i.setLayoutParams(new ImageSwitcher.LayoutParams(  
                                LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));  
                return i;  
        }

ステップ 9:

ギャラリーにアダプターを追加し、OnItemSelectedListener リスナーを追加します

g.setAdapter(new ImageAdapter(this));  
                g.setOnItemSelectedListener(this);

これですべては終了です。実行結果は次のとおりです

Android UI コントロール シリーズ: ギャラリー (ギャラリー ビュー)

完全なソース コード:

package org.hualang.Gallery;<br><br>import android.app.Activity;<br>import android.content.Context;<br>import android.os.Bundle;<br>import android.view.View;
<br>import android.view.ViewGroup;<br>import android.view.Window;<br>import android.view.animation.AnimationUtils;<br>import android.widget.AdapterView;<br>
import android.widget.BaseAdapter;<br>import android.widget.Gallery;<br>import android.widget.ImageSwitcher;<br>import android.widget.ImageView;<br>
import android.widget.AdapterView.OnItemSelectedListener;<br>import android.widget.Gallery.LayoutParams;<br>import android.widget.ViewSwitcher.ViewFactory;<br><br>
public class GalleryTest extends Activity implements OnItemSelectedListener,<br>                ViewFactory {<br><br>        
private ImageSwitcher mSwitcher;<br><br>        private Integer[] mThumbIds = { R.drawable.thumb0,<br>                        R.drawable.thumb1, R.drawable.thumb2,
<br>                        R.drawable.thumb3, R.drawable.thumb4,<br>                        R.drawable.thumb5, R.drawable.thumb6,<br>                        
R.drawable.thumb7 };<br><br>        private Integer[] mImageIds = { R.drawable.img0, R.drawable.img1,<br>                        R.drawable.img2, R.drawable.img3,
 R.drawable.img4,<br>                        R.drawable.img5, R.drawable.img6, R.drawable.img7 };<br><br>        @Override<br>        
 public void onCreate(Bundle savedInstanceState) {<br>                super.onCreate(savedInstanceState);<br><br>                
 requestWindowFeature(Window.FEATURE_NO_TITLE);<br>                setContentView(R.layout.main);<br>                
 mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);<br>                mSwitcher.setFactory(this);<br>                
 mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,<br>                                android.R.anim.fade_in));<br>                
 mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,<br>                                android.R.anim.fade_out));<br><br>                
 Gallery g = (Gallery) findViewById(R.id.gallery);<br><br>                g.setAdapter(new ImageAdapter(this));
                g.setOnItemSelectedListener(this);<br><br>        }<br><br>        public class ImageAdapter extends BaseAdapter {<br>                
                public ImageAdapter(Context c) {<br>                        mContext = c;<br>                }<br>                public int getCount() {
                <br>                        return mThumbIds.length;<br>                }<br>                public Object getItem(int position) {
                <br>                        return position;<br>                }<br>                public long getItemId(int position) {
                <br>                        return position;<br>                }<br>                public View getView(int position, View convertView, ViewGroup 
                parent) {<br>                        ImageView i = new ImageView(mContext);<br><br>                        i.setImageResource(mThumbIds[position]);
                <br>                        i.setAdjustViewBounds(true);<br>                        
                i.setLayoutParams(new Gallery.LayoutParams(<br>                                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                <br>                        i.setBackgroundResource(R.drawable.picturefrom);<br>                        return i;<br>                
                }<br>                private Context mContext;<br>        }<br><br>        @Override<br>        public void onItemSelected(AdapterView<?> adapter, 
                View v, int position,<br>                        long id) {<br>                mSwitcher.setImageResource(mImageIds[position]);<br>        }<br>
                <br>        @Override<br>        public void onNothingSelected(AdapterView<?> arg0) {<br><br>        }<br><br>        @Override<br>        
                public View makeView() {<br>                ImageView i = new ImageView(this);<br>                i.setBackgroundColor(0xFF000000);
                <br>                i.setScaleType(ImageView.ScaleType.FIT_CENTER);<br>                i.setLayoutParams(new ImageSwitcher.LayoutParams(<br>      
                                          LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));<br>                return i;<br>        }<br>}
rree

上記は Android UI コントロール シリーズ: ギャラリー (ギャラリー ビュー) コンテンツです。その他の関連コンテンツについては、PHP 中国語 Web サイト (www.php.cn) にご注意ください。


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。