前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android 画廊 重叠(gallery是什么)

Android 画廊 重叠(gallery是什么)

作者头像
全栈程序员站长
发布2022-07-28 21:11:26
1K0
发布2022-07-28 21:11:26
举报

大家好,又见面了,我是你们的朋友全栈君。

mainActiviy

代码语言:javascript
复制
package com.example.mygallary;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class MainActivity extends Activity {
	Gallery gal;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageAdapter adapter=new ImageAdapter(this);
        gal=(Gallery)findViewById(R.id.gal);
        gal.setAdapter(adapter);
        gal.setSelection(Integer.MAX_VALUE/2);
        
        
        
        
    }
    
    public  class  ImageAdapter  extends BaseAdapter{
    	Context context=null;
    	int mGalleryItemBackground;
    	public ImageAdapter(Context context){
    		this.context=context;
  	TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);//加载资源文件
    		 mGalleryItemBackground = a.getResourceId(
    		 R.styleable.HelloGallery_android_galleryItemBackground, 0);//设置Gallery的背景布局
    		                 a.recycle();
    	}
    	
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return Integer.MAX_VALUE;
		}
		int ids[]={R.drawable.s1,R.drawable.s2,R.drawable.s3,R.drawable.s4,R.drawable.s5,R.drawable.s6};

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return ids[position%6];
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position%6;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			ImageView imageView=null;
			if(convertView==null){
				imageView=new ImageView(context);
				convertView=imageView;
			}
			imageView.setImageResource(ids[position%6]);
			imageView.setBackgroundResource(mGalleryItemBackground);
			imageView.setLayoutParams(new Gallery.LayoutParams(260,220));
			imageView.setScaleType(ImageView.ScaleType.FIT_XY);
			return imageView;
		}
    	
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

values 包下attr.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<resources>
   <declare-styleable name="HelloGallery">
     <attr name="android:galleryItemBackground" />
   </declare-styleable>
</resources>

drawable包下6张图片S1 S2…

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/129017.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年4月1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档