Gallery和ImageSwitcher怎么在Android中使用

Gallery和ImageSwitcher怎么在Android中使用?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),扶風(fēng)企業(yè)網(wǎng)站建設(shè),扶風(fēng)品牌網(wǎng)站建設(shè),網(wǎng)站定制,扶風(fēng)網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,扶風(fēng)網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

布局文件activity_main.xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context=".MainActivity" >
  <ImageSwitcher
    android:id="@+id/imageSwitcher"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:paddingTop="30px" >
  </ImageSwitcher>
  <Gallery
    android:id="@+id/gallery1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:spacing="5px"
    android:unselectedAlpha="0.6" />
</RelativeLayout>

MainActivity.java代碼如下:

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
public class MainActivity extends Activity {
  private int imageId[] = new int[] { R.drawable.a, R.drawable.b,
      R.drawable.c, R.drawable.d, R.drawable.e, R.drawable.f,
      R.drawable.g, R.drawable.h, R.drawable.i, R.drawable.j,
      R.drawable.k };
  private ImageSwitcher imageSwitcher;
  private Gallery gallery;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imageSwitcher = (ImageSwitcher) this.findViewById(R.id.imageSwitcher);
    gallery = (Gallery) this.findViewById(R.id.gallery1);
    // 設(shè)置動(dòng)畫效果
    imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
        android.R.anim.fade_in));
    imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
        android.R.anim.fade_out));
    imageSwitcher.setFactory(new ViewFactory() {
      @Override
      public View makeView() {
        ImageView imageView = new ImageView(MainActivity.this);
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); // 設(shè)置保持縱橫比居中
        imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        return imageView;
      }
    });
    GalleryAdapter adapter = new GalleryAdapter(MainActivity.this,imageId);
    gallery.setAdapter(adapter);
    gallery.setSelection(imageId.length / 2);
    gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
      @Override
      public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {
        imageSwitcher.setImageResource(imageId[position]);
      }
      @Override
      public void onNothingSelected(AdapterView<?> arg0) {
      }
    });
  }
}

其中需要的一個(gè)適配器:

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 GalleryAdapter extends BaseAdapter {
  private int[] imageId;
  private Context mContext;
  /**
   * 穿入上下文和圖片資源數(shù)組
   * @param mContext
   * @param imageId
   */
  public GalleryAdapter(Context mContext, int[] imageId) {
    this.mContext = mContext;
    this.imageId = imageId;
  }
  @Override
  public int getCount() {
    return imageId.length;
  }
  @Override
  public Object getItem(int position) {
    return imageId[position];
  }
  @Override
  public long getItemId(int position) {
    return position;
  }
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView1;
    if (convertView == null) {
      imageView1 = new ImageView(mContext);
      imageView1.setScaleType(ImageView.ScaleType.FIT_XY);
      imageView1.setLayoutParams(new Gallery.LayoutParams(180, 135));
      TypedArray typedArray = mContext
          .obtainStyledAttributes(R.styleable.Gallery);
      imageView1.setBackgroundResource(typedArray.getResourceId(
          R.styleable.Gallery_android_galleryItemBackground, 0));
      imageView1.setPadding(5, 0, 5, 0); // 設(shè)置ImageView的內(nèi)邊距
    } else {
      imageView1 = (ImageView) convertView;
    }
    imageView1.setImageResource(imageId[position]); // 為ImageView設(shè)置要顯示的圖片
    return imageView1; // 返回ImageView
  }
}

Android是什么

Android是一種基于Linux內(nèi)核的自由及開放源代碼的操作系統(tǒng),主要使用于移動(dòng)設(shè)備,如智能手機(jī)和平板電腦,由美國Google公司和開放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開發(fā)。

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。

分享標(biāo)題:Gallery和ImageSwitcher怎么在Android中使用
URL分享:http://muchs.cn/article38/gpjspp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、全網(wǎng)營銷推廣、、建站公司搜索引擎優(yōu)化、電子商務(wù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)