Gallery与Imageswitch完美结合 做相册一绝啊


今天看书的时候看到图片的处理 其实我们最关心的是图片的显示效果 用Gallery与Imageswitch完美结合 给人眼前一亮的清醒感 请看效果吧

 最侯就是要实现通过滑动上面的Gallery动态的在下面的Imageswitch控件里显示出来 

首先在main.xml里添加这两个控件

    <Gallery 
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:id="@+id/gallery_id"
        android:layout_marginTop="20dp"
        />
    <ImageSwitcher 
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:id="@+id/imageSwitcher_id"
        android:layout_marginTop="30dp"
        ></ImageSwitcher>


然后在MainActivity中的代码编写了

 

package com.example.gallery;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewSwitcher.ViewFactory;

public class MainActivity extends Activity implements ViewFactory , OnItemSelectedListener{

    private Gallery gallery;
    private ImageAdaper imageAdaper;
	private int[] imageIds =new int[]{
			R.drawable.pic1 , R.drawable.pic2,
			R.drawable.pic3 , R.drawable.pic4,
			R.drawable.pic5 , R.drawable.pic6,
			R.drawable.pic7 , R.drawable.pic8,
	};
	private ImageSwitcher imageSwitcher;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageAdaper = new ImageAdaper(this);
        gallery = (Gallery)findViewById(R.id.gallery_id);
        gallery.setOnItemSelectedListener(this);
        
        imageSwitcher = (ImageSwitcher)findViewById(R.id.imageSwitcher_id);
        imageSwitcher.setFactory(this);
        imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
        imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
        gallery.setAdapter(imageAdaper);
    }
	public class ImageAdaper extends BaseAdapter{
		
		Context context;
		int mGalleryItemBackground;
		public ImageAdaper(Context context){
			this.context = context;
			TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);
			mGalleryItemBackground = typedArray.getResourceId(
					R.styleable.Gallery_android_galleryItemBackground, 0);
		}

		
		public int getCount() {
			// TODO Auto-generated method stub
			return Integer.MAX_VALUE;
		}

		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return imageIds[position];
		}

		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ImageView imageview = new ImageView(this.context);
			imageview.setBackgroundResource(imageIds[position % imageIds.length]);
			imageview.setScaleType(ImageView.ScaleType.FIT_XY);
			imageview.setLayoutParams(new Gallery.LayoutParams(136 , 88));
			return imageview; 
		}
		
	}
	public View makeView() {
		// TODO Auto-generated method stub
		ImageView imageView = new ImageView(this);
		imageView.setScaleType(imageView.getScaleType().FIT_CENTER);
		imageView.setBackgroundColor(0xFF000000);
		imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT , LayoutParams.FILL_PARENT));
	
		return imageView;
	}
	public void onItemSelected(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		imageSwitcher.setImageResource(imageIds[position%imageIds.length]);
	}
	public void onNothingSelected(AdapterView<?> parent) {
		// TODO Auto-generated method stub
		
	}


    
}


这样就OK了 也行看看。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值