asfman
android developer
posts - 90,  comments - 213,  trackbacks - 0

main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation
="vertical"
    android:layout_width
="fill_parent"
    android:layout_height
="fill_parent"
    
>
    
<Gallery android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/gallery1"></Gallery>
    
<LinearLayout android:layout_width="fill_parent"
          android:layout_height
="wrap_content"
          android:layout_alignParentBottom
="true"
          android:id
="@+id/indicator"
          android:gravity
="center">
    
</LinearLayout>    
</RelativeLayout>
MainActivity.java
package com.asfman;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class GalleryTest extends Activity {
    
    
private Gallery gallery;
    
private int curPos = 0;
    
private Runnable runnable;
    
private int delayTime = 5000;
    
private Handler uiHandler = new Handler() {

        @Override
        
public void handleMessage(Message msg) {
            
if(curPos >= gallery.getCount()) curPos = 0;
            gallery.setSelection(curPos);
        }
        
    };
    
    @Override
    
public void onCreate(Bundle savedInstanceState) {
        
super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        gallery 
= (Gallery) findViewById(R.id.gallery1);
        ImageAdapter imageAdapter 
= new ImageAdapter(this);
        gallery.setAdapter(imageAdapter); 
        gallery.setOnItemSelectedListener(
new OnItemSelectedListener() {

            @Override
            
public void onItemSelected(AdapterView<?> arg0, View view,
                    
int position, long id) {
                Log.i(
"info""selected:" + position);
                LinearLayout oLayout 
= (LinearLayout) findViewById(R.id.indicator);
                
for(int i = 0; i < gallery.getAdapter().getCount(); i++) {
                    ImageView oView 
= ((ImageView)oLayout.getChildAt(i));
                    oView.setImageResource(position 
== i ? R.drawable.home_light_frame : R.drawable.home_dark_frame);
                }    
                uiHandler.removeCallbacks(runnable);
                uiHandler.postDelayed(runnable, delayTime);
                curPos 
= position;
            }
            @Override
            
public void onNothingSelected(AdapterView<?> arg0) {
                
            }
            
        });
        gallery.setOnItemClickListener(
new OnItemClickListener(){

            @Override
            
public void onItemClick(AdapterView<?> parent, View view,
                    
int position, long id) {
                Toast.makeText(GalleryTest.
this"positon:" + position, Toast.LENGTH_LONG).show();
            }

            });
        
//gallery.setSelection(1);
        
        LinearLayout oLayout 
= (LinearLayout) findViewById(R.id.indicator);
        
for(int i = 0; i < gallery.getAdapter().getCount(); i++) {
            ImageView oView 
= new ImageView(this);
            oView.setImageResource(R.drawable.home_dark_frame);
            oLayout.addView(oView);
        }         
        runnable 
= run();
        uiHandler.postDelayed(runnable, delayTime);
    }

    
private Runnable run() {
        
return new Runnable() {
            @Override
            
public void run() {
                uiHandler.sendEmptyMessage(
1);
                curPos
++;
            }
        };
    }
}
class ImageAdapter extends BaseAdapter
{
    
private Context mContext;
    
private int[] resIds = new int[]{ R.drawable.azk, R.drawable.jp, R.drawable.azk, R.drawable.jp, R.drawable.azk, R.drawable.jp};
    
public ImageAdapter(Context context)
    {
        mContext 
= context;
                     
    }
     
// 返回图像总数
    public int getCount()
    {
        
return resIds.length;
    }
    
public Object getItem(int position)
    {
        
return position;
    }

    
public long getItemId(int position)
    {
        
return position;
    }
     
// 返回具体位置的ImageView对象
    public View getView(int position, View convertView, ViewGroup parent)
    {
        Log.i(
"info""------------------getView:" + position);
        
if(convertView == null) {
            LayoutInflater layoutInflater 
= LayoutInflater.from(mContext);
            convertView 
= layoutInflater.inflate(R.layout.item, null);
        }
        
// 设置当前图像的图像(position为当前图像列表的位置)
        ImageView oImageView = (ImageView) convertView.findViewById(R.id.img);
        oImageView.setImageResource(resIds[position]);
        
        
return convertView;
    }
}
posted on 2011-08-04 14:19 汪杰 阅读(1641) 评论(0)  编辑 收藏 引用 所属分类: Java
只有注册用户登录后才能发表评论。

<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

常用链接

留言簿(15)

随笔分类(1)

随笔档案(90)

文章分类(727)

文章档案(712)

相册

收藏夹

http://blog.csdn.net/prodigynonsense

友情链接

最新随笔

搜索

  •  

积分与排名

  • 积分 - 466814
  • 排名 - 6

最新随笔

最新评论

阅读排行榜

评论排行榜