Android圖像切換器含代碼

Android圖像切換器

Android圖像切換器提供了一種在圖像之間實現過渡動畫的方式。愛掏網 - it200.com要使用圖像切換器,我們需要在.xml文件中實現 ImageSwitcher 組件。愛掏網 - it200.com

ImageSwitcher的 setFactory() 方法提供了一個接口的實現。愛掏網 - it200.comViewFactory接口實現了它的未實現方法并返回一個ImageView。愛掏網 - it200.com

圖像切換器示例

讓我們來實現一個圖像切換器。愛掏網 - it200.com

在layout文件夾中創建activity_main.xml和content_main.xml文件。愛掏網 - it200.com

將一些要切換的圖像放在drawable文件夾中。愛掏網 - it200.com

activity_main.xml

文件:activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.test.imageswitcher.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>
    <include layout="@layout/content_main" />

</android.support.design.widget.CoordinatorLayout>

content_main.xml

文件: content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.test.imageswitcher.MainActivity"
    tools:showIn="@layout/activity_main">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Image Switcher Example"

        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <ImageSwitcher
        android:id="@+id/imageSwitcher"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:layout_marginBottom="28dp"
        android:layout_marginTop="40dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Next"
        android:id="@+id/button"
        android:layout_marginBottom="47dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

Activity類

文件:MainActivity.java

package com.example.test.imageswitcher;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;

import android.app.ActionBar;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;


public class MainActivity extends AppCompatActivity {
    ImageSwitcher imageSwitcher;
    Button nextButton;

    int imageSwitcherImages[] = 
      {R.drawable.cpp, R.drawable.c_sarp, R.drawable.jsp, R.drawable.mysql, R.drawable.hadoop};

    int switcherImageLength = imageSwitcherImages.length;
    int counter = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
        nextButton = (Button) findViewById(R.id.button);

        imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                ImageView switcherImageView = new ImageView(getApplicationContext());
                switcherImageView.setLayoutParams(new ImageSwitcher.LayoutParams(
                        ActionBar.LayoutParams.FILL_PARENT, ActionBar.LayoutParams.FILL_PARENT
                ));
                switcherImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                switcherImageView.setImageResource(R.drawable.hadoop);
                //switcherImageView.setMaxHeight(100);
                return switcherImageView;
            }
        });

        Animation aniOut = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);
        Animation aniIn = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);

       imageSwitcher.setOutAnimation(aniOut);
       imageSwitcher.setInAnimation(aniIn);

        nextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                counter++;
                if (counter == switcherImageLength){
                    counter = 0;
                    imageSwitcher.setImageResource(imageSwitcherImages[counter]);
                }
                else{
                    imageSwitcher.setImageResource(imageSwitcherImages[counter]);
                }
            }
        });
    }

}

輸出

聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。
發表評論
更多 網友評論0 條評論)
暫無評論

返回頂部

主站蜘蛛池模板: 欧美不卡在线视频| 免费看毛片电影| 亚洲AV无码成人专区| 91色视频在线| 波多野结衣被强女教师系列 | 992tv成人影院| 欧美zooz人禽交免费观看| 国产精品多人P群无码| 亚洲精品伊人久久久久| 99久久香蕉国产线看观香| k频道国产欧美日韩精品| 欧美性大战久久久久久| 国产精品久久久久影院| 亚洲乱码一区二区三区在线观看| 18禁无遮拦无码国产在线播放| 欧美在线暴力性xxxx| 好吊妞在线观看| 伊人热人久久中文字幕| a在线观看网站| 欧美黑人巨大xxxxx视频| 国内精品在线视频| 亚洲国产另类久久久精品黑人| 网址在线观看你懂的| 欧洲国产成人精品91铁牛tv| 国产成人综合久久久久久| 久久精品国产99国产精品澳门| 韩国高清色www在线播放| 日本www在线观看| 国产精品va在线观看无| 乱人伦xxxx国语对白| 贱妇汤如丽全篇小说| 欧乱色国产精品兔费视频| 国产成人久久久精品二区三区| 亚洲欧美中文字幕5发布| 18岁女人毛片| 日韩一区二区三区精品| 国产v亚洲v天堂a无码| 久久婷婷五月综合97色一本一本| 色综合热无码热国产| 好男人资源网在线看片| 亚洲精品国产电影|