安卓app_sl4_6图像切换器转换器

安卓app_sl4_6图像切换器转换器

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    
    tools:context="com.example.sl4_6.MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="上一张" />

    <ImageSwitcher
        android:id="@+id/imageSwitcher1"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </ImageSwitcher>

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="下一张" />

</LinearLayout>
package com.example.sl4_6;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
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.img01,
            R.drawable.img02,
            R.drawable.img03,
            R.drawable.img04,
            R.drawable.img05,
            R.drawable.img06,
            R.drawable.img07,
            R.drawable.img08,
            R.drawable.img09};
    private int index = 0;
    private ImageSwitcher imageSwitcher;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageSwitcher=(ImageSwitcher) findViewById(R.id.imageSwitcher1);
        //设置动画效果
        //设置淡入动画
        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() {
                // TODO Auto-generated method stub
                ImageView imageView=new ImageView(MainActivity.this);
                //设置保持纵横比居中缩放图像
                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
                        LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
                return imageView;
            }
            
        });
        imageSwitcher.setImageResource(imageId[index]);
        Button up=(Button)findViewById(R.id.button1);
        Button down=(Button)findViewById(R.id.button2);
        up.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(index>0)
                {
                    index--;
                }
                else
                {
                    index=imageId.length-1;
                }
                imageSwitcher.setImageResource(imageId[index]);
            }
            
        });
        down.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(index<imageId.length-1)
                {
                    index++;
                }
                else
                {
                    index=0;
                }
                imageSwitcher.setImageResource(imageId[index]);
            }
            
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

 

posted @ 2022-02-28 13:07  txwtech  阅读(42)  评论(0编辑  收藏  举报