热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Android基础——高级UI组件:图像视图和图片切换器

layout代码

layout代码

xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/a"
        />
    
    <ImageView
        android:layout_width="70dp"
        android:layout_height="100dp"
        android:background="#FF0000"
        android:src="@drawable/a"
        />

    
    <ImageView
        android:layout_width="70dp"
        android:layout_height="100dp"
        android:scaleType="fitXY"
        android:src="@drawable/a"
        android:tint="#66FF0000"
        />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxHeight="90dp"
        android:minWidth="200dp"
        android:src="@drawable/a"
        android:adjustViewBounds="true"
        />

    <ImageSwitcher
        android:id="@+id/imageswitcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        />

LinearLayout>

java调用代码

package com.example.myhighuii;

import androidx.appcompat.app.AppCompatActivity;

import android.media.Image;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;

public class MainActivity extends AppCompatActivity {

    ImageSwitcher imageSwitcher = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageSwitcher = (ImageSwitcher) findViewById(R.id.imageswitcher);
        //淡出效果
        imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(
                MainActivity.this,android.R.anim.fade_out
        ));
        //渐入效果
        imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(
                MainActivity.this,android.R.anim.fade_in
        ));

        //制定一个输出工厂
        imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                //为图像切换器指定图像
                ImageView imageView = new ImageView(MainActivity.this);
                //为其指定一张默认加载图片
                imageView.setImageResource(R.drawable.a);
                return imageView;
            }
        });

        //为图像切换器设置监听器
        imageSwitcher.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //参数v就是imageSwitcher
                ((ImageSwitcher)v).setImageResource(R.drawable.b);
            }
        });
    }
}

呈现效果

技术图片

多图片的循环切换器实现

xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <ImageSwitcher
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageSwitcher">
    ImageSwitcher>

LinearLayout>

java调用

package com.example.myhighuiii;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;

public class MainActivity extends AppCompatActivity {

   private int[] arrayPicture = new int[]{
           R.mipmap.a, R.mipmap.b, R.mipmap.c
   };
   private ImageSwitcher imageSwitcher = null;
   private int index = 0;
   private float touchDownX;//手指按下的X坐标
   private float touchUpX;//手指抬起的X坐标

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageSwitcher = (ImageSwitcher)findViewById(R.id.imageSwitcher);
        imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                ImageView imageView = new ImageView(MainActivity.this);
                imageView.setImageResource(arrayPicture[index]);//当前要显示的图片
                return imageView;
            }
        });

        //设置事件监听器
        imageSwitcher.setOnTouchListener(new View.OnTouchListener(){
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                //如果触发了按下事件
                if(event.getAction()==MotionEvent.ACTION_DOWN){
                    touchDownX = event.getX();
                    return true;
                }
                else if(event.getAction()==MotionEvent.ACTION_UP){//触发了抬起事件
                    touchUpX=event.getX();
                    if(touchUpX-touchDownX>100){//认为是从左向右滑动的,往前走一步
                        index= index==0 ? arrayPicture.length-1 : index-1;
                        //设置淡入淡出
                        imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(
                                MainActivity.this,android.R.anim.fade_in
                        ));
                        imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(
                                MainActivity.this,android.R.anim.fade_out
                        ));
                        //设置切换的图片资源
                        imageSwitcher.setImageResource(arrayPicture[index]);
                    }
                    else if(touchUpX-touchDownX<-100){//认为是从右向左滑动的
                        index = index==arrayPicture.length-1 ? 0 : index+1;
                        //设置淡入淡出
                        imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(
                                MainActivity.this,android.R.anim.fade_in
                        ));
                        imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(
                                MainActivity.this,android.R.anim.fade_out
                        ));
                        //设置切换的图片资源
                        imageSwitcher.setImageResource(arrayPicture[index]);
                    }
                    return true;
                }
                return false;
            }
        });
    }
}

Android基础——高级UI组件:图像视图和图片切换器


推荐阅读
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • 本文详细介绍了Linux中进程控制块PCBtask_struct结构体的结构和作用,包括进程状态、进程号、待处理信号、进程地址空间、调度标志、锁深度、基本时间片、调度策略以及内存管理信息等方面的内容。阅读本文可以更加深入地了解Linux进程管理的原理和机制。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • 本文讲述了作者通过点火测试男友的性格和承受能力,以考验婚姻问题。作者故意不安慰男友并再次点火,观察他的反应。这个行为是善意的玩人,旨在了解男友的性格和避免婚姻问题。 ... [详细]
  • 后台获取视图对应的字符串
    1.帮助类后台获取视图对应的字符串publicclassViewHelper{将View输出为字符串(注:不会执行对应的ac ... [详细]
author-avatar
手机用户2602935395
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有