Android--Gallery,画廊

运行截图:

 

 

set按钮可将当前图片设为壁纸

 

Java代码:

 

package com.mrzhu.test0109;

import java.io.IOException;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;

public class GalleryTest extends Activity {
 
 private Gallery gallery;
 private ImageView image;
 private Button set;//此按钮用于调用setWallpaper()方法,将当前显示的图片设为壁纸
 private int current;//保存当前的position,用于从数组中取得图片资源
 //整型数组,用于存储图片的id
 private int[] arr_image = {R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4
   ,R.drawable.p5, R.drawable.p6};
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.gallerymain);
  
  
  gallery = (Gallery) findViewById(R.id.gallery);
  image = (ImageView) findViewById(R.id.image);
  set = (Button) findViewById(R.id.set);

  //实例化一个适配器对象
  GalleryAdapter gallAdapter = new GalleryAdapter();
  gallery.setAdapter(gallAdapter);
  //设置监听,从中可取得position
  gallery.setOnItemSelectedListener(new GallerysetOnItemSelectedListener());
  //为按钮设置监听,将当前辐图片设为壁纸
  set.setOnClickListener(new SetWallpaper());
 }

 //button按钮的监听
 private class SetWallpaper implements OnClickListener{
  @Override
  public void onClick(View v) {
   //将图片资源转换为Bitmap对象
   Bitmap bitmap = BitmapFactory.decodeResource(getResources(), arr_image[current]);
   try {
    //调用方法将当前图片设为壁纸,需要申请权限
//    <uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission>
    setWallpaper(bitmap);
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }

 private class GallerysetOnItemSelectedListener implements OnItemSelectedListener{

  @Override
  public void onItemSelected(AdapterView<?> arg0, View arg1, int position,
    long arg3) {
   //此处的current为全局变量
   current = position;
   //将当前的图片显示到上面大的ImageView中
   image.setImageResource(arr_image[position]);
  }
  @Override
  public void onNothingSelected(AdapterView<?> arg0) {
   
  }
 }
 
 //自定义的适配器
 private class GalleryAdapter extends BaseAdapter{

  @Override
  public int getCount() {
   return arr_image.length;
  }

  @Override
  public Object getItem(int position) {
   return null;
  }

  @Override
  public long getItemId(int position) {
   return 0;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
   //实现一个转换器,
   LayoutInflater lay = getLayoutInflater();
//   将xml转换为View
   View gall = lay.inflate(R.layout.gallery, null);
   //取出控件并设置显示的图片
   ImageView image = (ImageView) gall.findViewById(R.id.image);
   image.setImageResource(arr_image[position]);
   //返回View对象
   return gall;
  }

 }
}


 

gallerymain.xml代码:

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">
  <ImageView
   android:layout_width="fill_parent"
   android:layout_height="200px"
   android:id="@+id/image"
  />
  
  <Gallery
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:id="@+id/gallery"
  />
  
  <Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="set"
   android:id="@+id/set"
  />
</LinearLayout>


 

 

gallery.xml代码:

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  
  <ImageView
   android:layout_width="200px"
   android:layout_height="wrap_content"
   android:id="@+id/image"
  />
</LinearLayout>


 

 

资源下载地址:http://download.csdn.net/detail/zlqqhs/4978911

包括ListView(列表) Gallery(画廊 ) AutoCompleteTextView(自动提示)与适配器

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值