首页 > 其他分享 >ImageSwitcher&Gallery练习

ImageSwitcher&Gallery练习

时间:2023-04-07 10:05:20浏览次数:44  
标签:widget image 练习 public drawable import android ImageSwitcher Gallery


先看图再说:




布局如下:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    
    <ImageSwitcher android:id="@+id/switcher"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
    />
    
    <Gallery android:id="@+id/gallery"
        android:background="#55000000"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        
        android:gravity="center_vertical"
        android:spacing="5dp"
    />
</RelativeLayout>




View:


package com.dc.app;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ViewSwitcher.ViewFactory;

import com.dc.adapter.ImageAdapter;

public class App extends Activity implements ViewFactory{
	
	ImageAdapter adapter;
	ImageSwitcher switcher;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        switcher=(ImageSwitcher)findViewById(R.id.switcher);
        switcher.setFactory(this);
      //系统的anim中的fade_in.xml 
        switcher.setInAnimation(AnimationUtils.loadAnimation(this, 
                android.R.anim.fade_in)); 
        //系统的anim中的fade_out.xml 
        switcher.setOutAnimation(AnimationUtils.loadAnimation(this, 
                android.R.anim.fade_out)); 
        
        Gallery gallery=(Gallery)findViewById(R.id.gallery);
        adapter=new ImageAdapter(this);
        switcher.setImageResource(adapter.images[0]); 
        gallery.setAdapter(adapter);
//        gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
//
//			@Override
//			public void onItemSelected(AdapterView<?> parent, View view, int position,
//					   long id) {
//				// TODO Auto-generated method stub
//				  switcher.setImageResource(adapter.images[position]); 
//			}
//
//			@Override
//			public void onNothingSelected(AdapterView<?> parent) {
//				// TODO Auto-generated method stub
//				Toast.makeText(App.this, "please select one", Toast.LENGTH_LONG).show();
//			}
//		});
        gallery.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					   long id) {
				// TODO Auto-generated method stub
				switcher.setImageResource(adapter.images[position]); 
			}
		});
        
       
        
    }

	@Override
	public View makeView() {
		// TODO Auto-generated method stub
		return new ImageView(this);
	}
}



适配器:


package com.dc.adapter;

import com.dc.app.R;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {

	Context context;
	public Integer[] images={
			R.drawable.icon,
			R.drawable.image_0,
			R.drawable.image_1,
			R.drawable.image_2,
			R.drawable.image_3,
			R.drawable.image_4,
			R.drawable.image_5,
			R.drawable.image_6,
			R.drawable.image_7,
			R.drawable.image_8,
			R.drawable.image_9,
			};
	
	public ImageAdapter(Context context){
		this.context=context;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return images.length;
	}

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

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

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ImageView imageView=new ImageView(context);
		imageView.setImageResource(images[position]);
//		imageView.setLayoutParams(new Gallery.LayoutParams(120, 100));
		imageView.setMaxWidth(120);
		imageView.setMaxHeight(100);
		imageView.setAdjustViewBounds(true);
		imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
		return imageView;
	}

}



  • ImageSwitcher&Gallery练习_ide

  • 大小: 119.5 KB
  • 查看图片附件

标签:widget,image,练习,public,drawable,import,android,ImageSwitcher,Gallery
From: https://blog.51cto.com/u_5454003/6174590

相关文章

  • SQLiteOpenHelper&SharedPreferences练习
    目录结构:packagecom.dc.app;importjava.text.DecimalFormat;importjava.util.Locale;importandroid.app.Activity;importandroid.app.AlertDialog;importandroid.app.Dialog;importandroid.app.Notification;importandroid.app.Notificati......
  • 循环语句练习(2)
    1.数组项目求和点击查看代码intsum=0;int[]s=newint[6]{2,3,4,7,9,6};foreach(intiins){sum+=i;}Console.WriteLine(sum);2.数组项求最大值和最小值......
  • 结对编程——四则运算练习题
    结对编程题目如下:小学老师要每周给同学出300道四则运算练习题。这个程序有很多种实现方式:C/C++C#/VB.net/JavaExcelUnixShellEmacs/Powershell/VbscriptPerlPython一个或两个运算符(a+b或a+b+c),100以内的数字,不需要写答案。需要检查答案是否正确,并且保证答案在0......
  • Gallery默认第一项居左解决方案
    网上找的,暂时用不到没测试过。Android中的Gallery控件默认会将第一项居中显示,在某些场景会影响用户体验,分享一下居左的解决方案:/***Alignthefirstgalleryitemtotheleft.**@paramparentViewTheviewcontainingthegallerywidget(weass......
  • AIArena Frontend 初步练习
    尝试对starter项目的页面进行改变修改侧边栏,只留下最上面的「仪表盘」和「列表页」两个大模块inSideNav.vuethecodeforthesidebarmenuis:<menu-content:navData="menu"/>fromtherewecangettoMenuContent.vueIthinkitisreferencingtheitemsinalis......
  • Python小练习:权重初始化(Weight Initialization)
    Python小练习:权重初始化(WeightInitialization)作者:凯鲁嘎吉-博客园 http://www.cnblogs.com/kailugaji/调用Pytorch中的torch.nn.init.xxx实现对模型权重与偏置初始化。1.weight_init_test.py1#-*-coding:utf-8-*-2#Author:凯鲁嘎吉CoralGajic3#https://w......
  • 链表练习4
    已知整型链表,设计算法,删除所有结点值为x的结点,删除的结点个数通过形参返回。#include<stdio.h>#include<stdlib.h>typedefstructnode{intdata;structnode*next;}Node,*LinkList;LinkListcreate(){LinkListh,r,s;h=(LinkList)malloc(sizeof(Node));r=h;inta;sc......
  • Python小练习:处理字符串
    Python小练习:处理字符串作者:凯鲁嘎吉-博客园 http://www.cnblogs.com/kailugaji/介绍两种处理字符串的方式:1.将英语名词单数转化为复数形式(仅适用于一般形式),2.将字符串(带有下换线_)转化为驼峰化形式。1.word_test.py1#-*-coding:utf-8-*-2#Author:凯鲁嘎吉......
  • 2023_4_5 蓝桥杯练习
    《P8671[蓝桥杯2018国AC]约瑟夫环 循环(%)问题》   我尝试用STL中的list与vector进行模拟可以发现复杂度都是>=O(N*K)的,会超时只有通过数学推式来得到全部正确的答案这里简单说一下STLlist与vector中对插入,删除元素的用法:list:......
  • NFS练习题
    NFS练习题1.开放/nfs/share目录,提供给任意用户只读(/etc/exportsro)查询1.任意客户端2.任意的用户​​​​ 服务端showmoutexportfssystemctlstartnfs 修改了nfs配置文件,需要重启什么吗?修改了nfs配置文件,只需要让nfs重新读取该配置文件即可,你都不需要重新,因为你......