拖动效果(Gallery)

新建一View,清单如下:

view_gallery.xml

 

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

 

 

 

 

 

 

    在面板拖拽图标,然后更改相关属性(Properties),如下:

 

<Gallery
        android:id="@+id/gallery02"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:layout_marginTop="30dp"/>

 

 

 

 

 

 

添加一按钮,如下:

 

<Button
        android:id="@+id/btnReturn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="166dp"
        android:text="@string/btn1Caption" />

 

 

 

 

 

 

    整体布局效果如下:

 

   把“Push me”按钮,更改为切换按钮。即实现切换功能。相关代码请参考多个View切换!

 

    实现Gallery需要继承BaseAdapter,我们命名为ImgAdapter。

    代码清单如下:

 

package com.example.prjandroid;

import android.content.Context;
import android.content.res.TypedArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class ImgAdapter extends BaseAdapter {
			
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return null;
	}

	@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
		
		return null;
	}

}

 

 

 

 

 

 

如果想要放图片,必须使用ImageView!

这时,我们把图片放到res/drawable下(建议使用png格式文件)。

然后,再新建一xml文件,提供Gallery的背景。如下:

res/attrs.xml

 

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="galleryThem">
        <attr name="android:galleryItemBackground"/>
    </declare-styleable>
</resources>

 

 

 

 

 

 

声明一图片ID的数组:

 

// resource draw
	private int[] resPics = new int[] {
		R.drawable.emacs1,
		R.drawable.emacs2,
		R.drawable.emacs3,
		R.drawable.emacs4,
		R.drawable.emacs5,
		R.drawable.emacs6,
		R.drawable.emacs7,
		R.drawable.emacs8,
		R.drawable.emacs9,
		R.drawable.emacs10
	};

 

 

 

 

 

 

在getView()方法中使用ImageView:

    

ImageView imgView = new ImageView(m_context);
		imgView.setImageResource(resPics[position]);
		imgView.setScaleType(ImageView.ScaleType.FIT_XY);
		imgView.setLayoutParams(new Gallery.LayoutParams(163, 106));
		
		imgView.setBackgroundResource(m_galleryItemBackGround);
		
		return imgView;

 

 

 

其中,ImageView.ScaleType共八种:

1·ImageView.ScaleType.center:图片位于视图中间,但不执行缩放。

2·ImageView.ScaleType.CENTER_CROP 按统一比例缩放图片(保持图片的尺寸比例)便于图片的两维(宽度和高度)等于或者大于相应的视图的维度

3·ImageView.ScaleType.CENTER_INSIDE按统一比例缩放图片(保持图片的尺寸比例)便于图片的两维(宽度和高度)等于或者小于相应的视图的维度

4·ImageView.ScaleType.FIT_CENTER缩放图片使用center

5·ImageView.ScaleType.FIT_END缩放图片使用END

6·ImageView.ScaleType.FIT_START缩放图片使用START

7·ImageView.ScaleType.FIT_XY缩放图片使用XY

8·ImageView.ScaleType.MATRIX当绘制时使用图片矩阵缩放

 

还有就是Context相当于windows中的Handle。

 

我们在构造这个类时,需要Context和初始化Gallery的属性,其相关代码如下:

 

public ImgAdapter(Context context) {
		// TODO Auto-generated constructor stub
		m_context = context;
		TypedArray typeArray = 
				m_context.obtainStyledAttributes(R.styleable.galleryThem);
		m_galleryItemBackGround = typeArray.getResourceId(
				R.styleable.galleryThem_android_galleryItemBackground, 0);
	}

 

 

 

 

 

基本上这个适配类就是这个样子了。

 

 

调用代码和之前讲的Spinner比较像!区别在于Spinner直接就传的this,但是在必须传主类.this(例如MainActivity.this)。

 

运行效果如下:

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值