Android _ Gallery

MainActivity.java

import java.util.ArrayList;


import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageView.ScaleType;

public class MainActivity extends Activity {
 private ArrayList<Drawable> imgList = new ArrayList<Drawable>();
 private Gallery gallery = null;
 @SuppressWarnings("deprecation")
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  InitImgList();
  gallery = (Gallery) findViewById(R.id.banner_gallery);
  gallery.setAdapter(new ImageAdapter(this));
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }
 private void InitImgList() {
  // 加载图片数据(本demo仅获取本地资源,实际应用中,可异步加载网络数据)
  imgList.add(this.getResources().getDrawable(R.drawable.img1));
  imgList.add(this.getResources().getDrawable(R.drawable.img2));
  imgList.add(this.getResources().getDrawable(R.drawable.img3));
  imgList.add(this.getResources().getDrawable(R.drawable.img4));
  imgList.add(this.getResources().getDrawable(R.drawable.img5));
  imgList.add(this.getResources().getDrawable(R.drawable.img6));
     }

     public class ImageAdapter extends BaseAdapter {
  private Context _context;

  public ImageAdapter(Context context) {
      _context = context;
  }

  public int getCount() {
      return imgList.size();
  }

  public Object getItem(int position) {

      return position;
  }

  public long getItemId(int position) {
      return position;
  }

  @SuppressWarnings("deprecation")
  public View getView(int position, View convertView, ViewGroup parent) {
      ImageView imageView = new ImageView(_context);
      imageView.setImageDrawable(imgList.get(position));
      imageView.setAdjustViewBounds(true);
      imageView.setScaleType(ScaleType.FIT_XY);
      imageView.setLayoutParams(new Gallery.LayoutParams(
       LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
      return imageView;
  }
     }
}

 

MyGallery.java

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Gallery;

@SuppressWarnings("deprecation")
public class MyGallery extends Gallery {

    
    private final int THRESHOLD = 600;

    public MyGallery(Context paramContext) {
 super(paramContext);
    }

    public MyGallery(Context paramContext, AttributeSet paramAttributeSet) {
 super(paramContext, paramAttributeSet);
    }

    public MyGallery(Context paramContext, AttributeSet paramAttributeSet,
     int paramInt) {
 super(paramContext, paramAttributeSet, paramInt);
    }

    private boolean isScrollingLeft(MotionEvent paramMotionEvent1,
     MotionEvent paramMotionEvent2) {
 float f2 = paramMotionEvent2.getX();
 float f1 = paramMotionEvent1.getX();
 if (f2 > f1)
     return true;
 return false;
    }

    public boolean onFling(MotionEvent paramMotionEvent1,
     MotionEvent paramMotionEvent2, float paramFloat1, float paramFloat2) {
 float f;
 if (isScrollingLeft(paramMotionEvent1, paramMotionEvent2)) {
     f = THRESHOLD;
 } else {
     f = THRESHOLD * -1;
 }
 return super.onFling(paramMotionEvent1, paramMotionEvent2, f,
  paramFloat2);
    }
}

 

activity_main.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="
http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <com.example.android_1500_gallery.MyGallery
        android:id="@+id/banner_gallery"
        android:layout_width="fill_parent"
        android:layout_height="257px"
        android:background="#000"
        android:fadingEdgeLength="0.0dp"
        android:spacing="1.0px" />

</LinearLayout>

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值