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


相关推荐

  • TDD 与FDD 的区别

    TDD 与FDD 的区别我们知道FDD(FrequenceDivisionDuplex)和TDD(TimeDivisionDuplex)分别是频分双工和时分双工的英文缩写。FDD系统是指系统的发送和接收数据使用不同的频率,在上行和下行频率之间有双工间隔,如GSM、CDMA、WCDMA系统都是典型的FDD系统;时分双工系统则是系统的发送和接收使用相同的频段,上下行数据发送在时间上错开,通过在不同时隙发送上下行数据可

    2022年5月22日
    45
  • matlab求两向量夹角_MATLAB基础练习(一)

    matlab求两向量夹角_MATLAB基础练习(一)1、按要求写出实现该功能的代码(1)使用方括号“[]”操作符产生一个列向量x,内容为1,2,4,7(2)使用方括号“[]”操作符产生一个行向量x,内容为1,2,4,7(3)使用冒号“:”操作符产生一个行向量x,内容为9,7,5,3,1(4)使用方括号“[]”操作符产生一个二维数组A,第1行为9,4,5,1;第2行为1,0,4,7(5)使用zeros函数产生一个3*2的二维数组A,使用one…

    2022年8月30日
    4
  • Vue(6)v-on指令的使用[通俗易懂]

    Vue(6)v-on指令的使用[通俗易懂]v-on监听事件可以用v-on指令监听DOM事件,并在触发时运行一些JavaScript代码。事件代码可以直接放到v-on后面,也可以写成一个函数。示例代码如下:<divid

    2022年7月29日
    11
  • CSDN帐号被盗尚未?

    CSDN帐号被盗尚未?

    2022年1月6日
    41
  • 什么是债券久期[通俗易懂]

    什么是债券久期[通俗易懂]在做债券的投资分析中经常出现的一个词汇——债券久期,之前更多地是专注于开发,并不明白数字背后的业务含义,今天特意梳理下并做个记录。百度百科的解释:久期也称持续期,是1938年由F.R.Macaula

    2022年8月3日
    3
  • android之activity中onSaveInstanceState和onRestoreInstanceState触发时机

    先看Application Fundamentals上的一段话:  Android calls onSaveInstanceState() before the activity becomes vulnerable to being destroyed by the system, but does not bother calling it when the instance is a

    2022年3月10日
    38

发表回复

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

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