本文主要是介绍89 Android Gallery 简单的画廊效果,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
activity_main.xml (布局文件代码)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity" ><!-- android:unselectedAlpha 设置选中的图片的透明度 ,android:spacing 图片之间的空白大小 --><Gallery android:id="@+id/gallery"android:layout_width="fill_parent"android:layout_height="fill_parent"android:spacing="10dip"android:unselectedAlpha="1"/>
</RelativeLayout>
GalleryAdpter.java适配器代码
package com.example.gallerymethod;import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
/*** 适配器* @author Administrator**/
public class GalleryAdpter extends BaseAdapter {Context context;int[] imgIds={R.drawable.a2,R.drawable.a5,R.drawable.denglong,R.drawable.huabi,R.drawable.yinfu};public GalleryAdpter(Context context){this.context=context;}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn imgIds.length;}@Overridepublic Object getItem(int arg0) {// TODO Auto-generated method stubreturn null;}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubImageView imageView=new ImageView(context);imageView.setImageResource(imgIds[position]);imageView.setScaleType(ImageView.ScaleType.FIT_XY);return imageView;}}
MainActivity.java
package com.example.gallerymethod;import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Gallery;public class MainActivity extends Activity {@SuppressWarnings("deprecation")private Gallery gallery;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);gallery=(Gallery)findViewById(R.id.gallery);GalleryAdpter galleryAdpter=new GalleryAdpter(MainActivity.this);gallery.setAdapter(galleryAdpter);}@Overridepublic 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;}}
这篇关于89 Android Gallery 简单的画廊效果的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!