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)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • Redis学习笔记—-Redis5.0.5配置文件详解「建议收藏」

    Redis学习笔记—-Redis5.0.5配置文件详解「建议收藏」4、解析配置文件(redis5.0.5)配置文件在${PATH}/redis.conf4.1、存储#当你需要为某个配置项指定内存大小的时候,必须要带上单位,通常的格式就是1k5gb4m等:#1k=>1000bytes#1kb=>1024bytes#1m=>1000000bytes#1mb=>10241024bytes#1g…

    2022年6月5日
  • Android视频直播的实现(包括服务器搭建)

    Android视频直播的实现(包括服务器搭建)最近一段时间,视频直播可谓大火。在视频直播领域,有不同的商家提供各种的商业解决方案,包括软硬件设备,摄像机,编码器,流媒体服务器等。本文要讲解的是如何使用一系列免费工具,打造一套视频直播方案。视频直播流程视频直播的流程可以分为如下几步:采集—>处理—>编码和封装—>推流到服务器—>服务器流分发—>播放器流播放1.采集采集是整个视频推流过…

    2022年6月3日
  • 在 ASP.NET MVC 中充分利用 WebGrid

    在 ASP.NET MVC 中充分利用 WebGridStuartLeeks下载代码示例今年早些时候,Microsoft发布了ASP.NETMVC版本3(asp.net/mvc)以及一款名为WebMatrix的新产品(asp.net/webmatrix)。 该WebMatrix版本中提供了几个工作效率帮助组件,可以简化诸如图表和表格数据呈现等任务。 其中一个帮助组件是WebGrid,

    2022年10月5日
  • SLAM算法总结——经典SLAM算法框架总结

    SLAM算法总结——经典SLAM算法框架总结SLAM算法总结——经典SLAM算法框架总结SLAM算法总结——经典SLAM算法框架总结SLAM算法总结——经典SLAM算法框架总结从研究生接触SLAM算法到现在也有

    2022年6月30日
  • pandas中的drop函数_pandas replace函数

    pandas中的drop函数_pandas replace函数dropna()函数的作用是去除读入的数据中(DataFrame)含有NaN的行。如下:>>>df=pd.DataFrame({“name”:[‘Alfred’,’Batman’,’Catwoman’],”toy”:[np.nan,’Batmobile’,’Bullwhip’],”born”:[pd.NaT,pd.Timestamp(“1940-04-25”),

    2022年9月18日
  • json字符串数组转json数组

    json字符串数组转json数组当需要把一串字符串转成一个json数组,并遍历其中的内容时。首先要导入net.sf.json.JSONArray和net.sf.json.JSONObject两个jar包<dependency><groupId>net.sf.json-lib</groupId><artifactId>json-lib</artifactId><version>2.4</version><c

    2022年6月21日

发表回复

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

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