【Android】使用Gallery組件實現迴圈顯示映像

來源:互聯網
上載者:User

剛剛寫完博文http://blog.csdn.net/jueblog/article/details/12209969 之後,就想著能不能讓圖片迴圈播放呢?

實驗出一個簡便易行的方法。

Activity作如下改進:

只需修改第36行即可:

imagedemo.setImageDrawable(getResources().getDrawable(iViews[position%iViews.length]));

即:

package com.app.test01;import com.app.adapter.ImageAdapter;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.Gallery;import android.widget.ImageView;public class GalleryTest extends Activity{Gallery gallery;ImageView imagedemo;ImageAdapter iAdapter = new ImageAdapter(this);@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_gallery);gallery = (Gallery) findViewById(R.id.gallery1);imagedemo = (ImageView) findViewById(R.id.imagedemo);gallery.setAdapter(iAdapter);gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id) {// TODO Auto-generated method stubInteger[] iViews = iAdapter.getmImageIds();imagedemo.setImageDrawable(getResources().getDrawable(iViews[position%iViews.length]));}});}}


對BaseAdapter適配器做如下改進:

package com.app.adapter;import com.app.test01.R;import android.content.Context;import android.content.res.TypedArray;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageView;public class ImageAdapter extends BaseAdapter {    private Context mContext;    public ImageAdapter(Context c) {        mContext = c;    }    public int getCount() { /* 重寫方法getCount,傳回圖片數目總數 */        return Integer.MAX_VALUE;    }    public Object getItem(int position) {/* 重寫的getItem,傳回position */        return position;    }    public long getItemId(int position) {/*重寫方法getItemId,傳回position */         return position;    }    public View getView(int position, View convertView, ViewGroup parent) {        ImageView i = new ImageView(mContext);        i.setImageResource(mImageIds[position%mImageIds.length]);/* 設定圖片給imageView對象 */          i.setLayoutParams(new Gallery.LayoutParams(150,75));        return i;    }    public Integer[] getmImageIds() {return mImageIds;}public void setmImageIds(Integer[] mImageIds) {this.mImageIds = mImageIds;}private Integer[] mImageIds = {            R.drawable.image01,            R.drawable.image02,            R.drawable.image03,            R.drawable.image04,            R.drawable.image05,            R.drawable.image06,            R.drawable.image07,            R.drawable.image08,            R.drawable.image09,    };}

如你所見,僅需修改21行和34行。從而即可實現Gallery組件的迴圈播放。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.