国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android控件ImageSwitcher實(shí)現(xiàn)左右圖片切換功能

2019-12-12 06:18:28
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

ImageSwitcher類是ViewSwitcher類的子類,它實(shí)現(xiàn)的效果是在完成ImageView的切換并且?guī)в袆?dòng)畫效果。要使用這個(gè)類需要以下兩個(gè)步驟:

1)為ImageSwitcher類提供一個(gè)ViewFactory,該ViewFactory生成的View組件必須是ImageView。

2)需要切換的時(shí)候,只需要嗲用ImageSwitcher的setImageDrawable()、setImageResource()、setImageURL()方法即可實(shí)現(xiàn)切換。

activity_main.xml:

<LinearLayout 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:orientation="vertical"  tools:context=".MainActivity" >  <ImageSwitcher    android:id="@+id/imageswitcher"    android:layout_width="200dp"    android:layout_height="200dp"    android:layout_gravity="center_horizontal" />  <RelativeLayout    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >    <Button      android:id="@+id/back"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_alignParentLeft="true"      android:text="back" />    <Button      android:id="@+id/forward"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_alignParentRight="true"      android:text="forward" />  </RelativeLayout></LinearLayout>

Main_activity.java:

package com.example.android_imageswitcher1;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageSwitcher;import android.widget.ImageView;import android.widget.ViewSwitcher.ViewFactory;public class MainActivity extends Activity implements ViewFactory,    OnClickListener {  ImageSwitcher mImageSwitcher = null;  Button btn1, btn2;  int index = 0;  int[] resId = new int[9];  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    mImageSwitcher = (ImageSwitcher) this.findViewById(R.id.imageswitcher);    btn1 = (Button) this.findViewById(R.id.back);    btn2 = (Button) this.findViewById(R.id.forward);    btn1.setOnClickListener(this);    btn2.setOnClickListener(this);    mImageSwitcher.setFactory(this);    mImageSwitcher.setInAnimation(this, android.R.anim.slide_in_left);    mImageSwitcher.setOutAnimation(this, android.R.anim.slide_out_right);    initResources();    if (resId.length > 0) {      mImageSwitcher.setImageResource(resId[0]);    }  }  public void initResources() {    resId[0] = R.drawable.adobe;    resId[1] = R.drawable.android;    resId[2] = R.drawable.circle;    resId[3] = R.drawable.digg;    resId[4] = R.drawable.flower;    resId[5] = R.drawable.gmail;    resId[6] = R.drawable.imdb;    resId[7] = R.drawable.photo;    resId[8] = R.drawable.point;  }  @Override  public View makeView() {    return new ImageView(MainActivity.this);  }  @Override  public void onClick(View view) {    int action = view.getId();    switch (action) {    case R.id.back:      index--;      if (index < 0) {        index = resId.length - 1;      }      mImageSwitcher.setImageResource(resId[index]);      break;    case R.id.forward:      index++;      if (index > resId.length - 1) {        index = 0;      }      mImageSwitcher.setImageResource(resId[index]);      break;    default:      break;    }  }}

實(shí)現(xiàn)的效果如下:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)Android軟件編程有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 临沂市| 武隆县| 紫阳县| 扬州市| 兰州市| 荥经县| 宜春市| 巨鹿县| 龙州县| 博湖县| 土默特右旗| 济南市| 武冈市| 泾阳县| 襄垣县| 双城市| 晋城| 辛集市| 萨嘎县| 锦屏县| 新营市| 股票| 北川| 东乡| 武隆县| 甘南县| 临沂市| 谢通门县| 河源市| 惠州市| 洛扎县| 孟连| 临西县| 松原市| 江都市| 长子县| 海阳市| 皋兰县| 涡阳县| 天镇县| 孟州市|