Android 画廊 重叠(gallery是什么)

mainActiviypackagecom.example.mygallary;importandroid.app.Activity;importandroid.content.Context;importandroid.content.res.TypedArray;importandroid.os.Bundle;importandroid.view.Menu;imp

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

mainActiviy

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

<?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.net/129017.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • TCP之深入浅出send和recv

    TCP之深入浅出send和recv

    2021年8月27日
  • [Python知识图谱] 一.哈工大pyltp安装及中文分句、中文分词、导入词典基本用法

    [Python知识图谱] 一.哈工大pyltp安装及中文分句、中文分词、导入词典基本用法本系列文章主要结合Python语言实现知识图谱构建相关工程,具有一定创新性和实用性,非常希望各位博友交流讨论,相互促进成长。第一篇文章主要介绍哈工大pytltp工具,包括安装过程、中文分词、词性标注和实体识别等。基础性文章,希望对您有所帮助。

    2022年11月2日
  • 关键部分CCriticalSection使用

    关键部分CCriticalSection使用类CCriticalSection的对象表示一个“临界区”,它是一个用于同步的对象,同一时刻仅仅同意一个线程存取资源或代码区。临界区在控制一次仅仅有一个线程改动数据或其他的控制资源时很实用。比如,在链表中添加一个结点就仅仅同意一次一个线程进行。通过使用CCriticalSection对象来控制链表,就能够达到这个目的。它就像是一把钥匙,哪个线程获得了它就获得了执行线程的权力,而把其他线程统统堵…

    2022年7月20日
  • IJ实现侧边栏单独搜索

    IJ实现侧边栏单独搜索第一步任意点击一个第二步输入要搜索的单词

    2022年6月27日
  • xshell怎么退出vi_xshell5

    xshell怎么退出vi_xshell5最近在学习Linux时,初次使用Vi编辑模式编辑文本,但是编辑完成之后,不知道怎么退出编辑模式,然后在网上查找了一番,特此分享给各位老铁:下面总结一些vi退出命令,学习!进入编辑模式,按o进行编辑编辑结束,按ESC键跳到命令模式,然后输入退出命令::w 保存文件但不退出vi编辑:w! 强制保存,不退出vi编辑:wfile 将修改另存到file中,不退出vi…

    2022年9月30日
  • 三极管典型开关电路

    三极管典型开关电路1.基极必须串接电阻,保护基极,保护CPU的IO口。2.基极根据PNP或者NPN管子加上拉电阻或者下拉电阻。3.集电极电阻阻值根据驱动电流实际情况调整。同样基极电阻也可以根据实际情况调整。4.基极和发射极需要并联电阻,该电阻的作用是在输入呈高阻态时使晶体管可靠截止,极小值是在前级驱动使晶体管饱和时与基极限流电阻分压后能够满足晶体管的临界饱和,实际选择时会大大高于这个极小值,

    2022年9月15日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号