码迷,mamicode.com
首页 > 移动开发 > 详细

【Android实战】Gallary+ImageSwicther图片查看器

时间:2014-11-14 00:17:59      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:图片   布局   android   layout   

仿照现在各大新闻网站图片新闻的浏览模式,上面展示具体图片(ImageSwitch),下面是可以滑动的小图片(Gallery)。

其中需要注意的是ImageSwitch需要定义一个工厂返回的组件,并且可以设置动画效果。

Gallery用适配器加载布局,其Gallery可设置,两个图片的间隔距离。

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageSwitcher
        android:id="@+id/switcher"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:layout_weight="1" >
    </ImageSwitcher>

    <Gallery
        android:id="@+id/gallary"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_margin="10dp"
        android:layout_weight="3"
        android:spacing="20dp" />

</LinearLayout>

java类:

package com.gallaryandswitch;

import android.app.Activity;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.ViewSwitcher.ViewFactory;

import com.example.wangyitest.R;

public class MainAct extends Activity {
	ImageSwitcher imageSwitcher;
	Gallery gallery;
	int[] pics = { R.drawable.f1, R.drawable.f2, R.drawable.f3, R.drawable.f4,
			R.drawable.f5, R.drawable.f6, R.drawable.f7, R.drawable.flower };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.galleryswitch);
		imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
		gallery = (Gallery) findViewById(R.id.gallary);
		imageSwitcher.setFactory(new ViewFactory() {

			@Override
			public View makeView() {
				// TODO Auto-generated method stub
				ImageView imageView = new ImageView(getApplicationContext());
				imageView.setScaleType(ScaleType.FIT_CENTER);
				imageView.setLayoutParams(new FrameLayout.LayoutParams(
						FrameLayout.LayoutParams.FILL_PARENT,
						FrameLayout.LayoutParams.FILL_PARENT));
				imageView.setBackgroundColor(getResources().getColor(
						R.color.bg3));
				return imageView;
			}
		});

		imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(
				getApplicationContext(), android.R.anim.fade_in));
		imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(
				getApplicationContext(), android.R.anim.fade_out));
		gallery.setAdapter(new MyAdapter());

		// gallery.setOnItemClickListener(new AdapterView.OnItemClickListener()
		// {
		//
		// @Override
		// public void onItemClick(AdapterView<?> parent, View view,
		// int position, long id) {
		// // TODO Auto-generated method stub
		// imageSwitcher.setImageResource(pics[position]);
		// }
		// });
		gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				imageSwitcher.setImageResource(pics[position]);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub

			}
		});

	}

	class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return pics.length;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

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

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ImageView imageView = new ImageView(getApplicationContext());
			imageView.setScaleType(ScaleType.FIT_CENTER);

			imageView.setLayoutParams(new Gallery.LayoutParams(400,
					Gallery.LayoutParams.WRAP_CONTENT));

			imageView.setImageBitmap(BitmapFactory.decodeResource(
					getResources(), pics[position]));
			imageView.setBackgroundColor(getResources().getColor(R.color.bg2));
			return imageView;

		}
	}

}


【Android实战】Gallary+ImageSwicther图片查看器

标签:图片   布局   android   layout   

原文地址:http://blog.csdn.net/scboyhj__/article/details/41091597

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!