android的ImageSwitcher控件及简单动画

最近菜鸟在学android,android的UI控件实在太多,也方便我们更好的优化界面以及内容,今天我学习的便是一个图片查看器,也就是ImageSwitcher,下面来说说今天的收获:

1、ImageSwiitcher和Gallery的布局

<ImageSwitcher
    android:id="@+id/switcher"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"/>

 <Gallery
     android:id="@+id/gallery"
     android:background="#55000000"
     android:layout_width="fill_parent"
     android:layout_height="60dp"
     android:layout_alignParentBottom="true"
     android:layout_alignParentLeft="true"
     android:gravity="center_vertical"
     android:spacing="16dp"/>
在一个相对布局里加入这两个控件,ImaheSwitch主要是进行图片查看,而Gallery画廊是对图片的进行展示,所以高度不能太高

2、动画效果

imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));
动画效果的话主要有这两个功能,一个是将动画插入进View,一个是取出,具体方法可以查看动画的设计方法

3、监听器

Gallery gallery=(Gallery)findViewById(R.id.gallery);		//定义一个画廊
gallery.setAdapter(new ImageAdapter(this));			//添加适配器
gallery.setOnItemSelectedListener(new onItemSelectedListener());	//给画廊添加选择监听器

private class onItemSelectedListener implements AdapterView.OnItemSelectedListener{
    public void onItemSelected(AdapterView<?>arg0,View arg1,int arg2,long arg3){	//arg就是position
        imageSwitcher.setImageResource(Images[arg2]);				//点击Item时便会展示这张图片
    }
    public void onNothingSelected(AdapterView<?>arg0){

    }
}
监听器的话还是比较简单的,只不过监听器种类还是比较多的,所以的注意执行的是哪种按钮的监听器

4、全部代码

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AnimationUtils;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;

public class MainActivity extends AppCompatActivity implements ViewSwitcher.ViewFactory{
    private ImageSwitcher imageSwitcher;
    private Integer[] Images={R.drawable.img_1,R.drawable.img_2,R.drawable.img_3};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);  //隐藏标题栏函数
        setContentView(R.layout.activity_main);
        imageSwitcher=(ImageSwitcher)findViewById(R.id.switcher);
        imageSwitcher.setFactory(this);          //实现ViewFactory接口并覆盖对应的makeView的方法
        imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
        imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));
        Gallery gallery=(Gallery)findViewById(R.id.gallery);
        gallery.setAdapter(new ImageAdapter(this));
        gallery.setOnItemSelectedListener(new onItemSelectedListener());
    }
    public View makeView(){ 				//重做View的展示方法
        ImageView imageView=new ImageView(this);
        imageView.setBackgroundColor(0xFF000000);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT
        ));
        return imageView;
    }
    public class ImageAdapter extends BaseAdapter{		//BaseAdapter:万能适配器
        private Context mContext;
        public ImageAdapter(Context context){
            mContext=context;
        }
        public int getCount(){
            return Images.length;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }
        public long getItemId(int position){
            return position;
        }
        public View getView(int position, View convertView, ViewGroup parent){  //对View的布局显示
            ImageView imageView=new ImageView(mContext);
            imageView.setImageResource(Images[position]);
            imageView.setAdjustViewBounds(true);
            imageView.setLayoutParams(new Gallery.LayoutParams(
                    WindowManager.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT
            ));
            imageView.setBackgroundResource(R.drawable.img_1);
            return imageView;
        }
    }
    private class onItemSelectedListener implements AdapterView.OnItemSelectedListener{
        public void onItemSelected(AdapterView<?>arg0,View arg1,int arg2,long arg3){
            imageSwitcher.setImageResource(Images[arg2]);
        }
        public void onNothingSelected(AdapterView<?>arg0){

        }
    }

}

总结:ImageSwitch是个图片查看器,加上画廊就像是QQ空间查看照片的模式一般,漂亮大方,同样,任何图片资源什么的都离不开Adapt,今天继承的这个BaseAdapter适配器是万能适配器,不管是往里面放图片文字各种信息都行,当然,这个程序还是比较简单的,当里面的照片过大时就容易出现卡顿,容易程序崩溃,不过我相信只要经过以后慢慢的进一步学习,我能优化这些问题。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值