Kotlin04.ImageView图片浏览器

支持查看图片的透明度,支持小区域查看图片

MainActivity.kt

package com.example.imageviewtest

import android.graphics.Bitmap
import android.graphics.drawable.BitmapDrawable
import android.media.Image
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.ImageView

class MainActivity : AppCompatActivity() {

    // 定义一个访问图片的数组
    private var images = intArrayOf(R.drawable.lijiang, R.drawable.qiao,
            R.drawable.shuangta, R.drawable.shui, R.drawable.xiangbi)
    // 定义默认显示的图片
    private var currentImg = 2
    // 定义图片的初始透明度
    private var alpha = 255

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val plus = findViewById<Button>(R.id.plus)
        val minus = findViewById<Button>(R.id.minus)
        val image1 = findViewById<ImageView>(R.id.image1)
        val image2 = findViewById<ImageView>(R.id.image2)
        val next = findViewById<Button>(R.id.next)
        next.setOnClickListener {
            image1.setImageResource(images[++currentImg % images.size])
        }
        // 定义改变图片透明度的方法
        val listener = View.OnClickListener {v ->
            if (v == plus) {
                alpha += 20
            }
            if (v == minus){
                alpha -= 20
            }
            if (alpha >= 255){
                alpha = 255
            }
            if (alpha <= 0){
                alpha = 0
            }
            image1.imageAlpha = alpha
        }
        plus.setOnClickListener(listener)
        minus.setOnClickListener(listener)
        image1.setOnTouchListener { v, event ->
            val bitmapDrawable = image1.drawable as BitmapDrawable
            val bitmap = bitmapDrawable.bitmap
            val scale = 1.0 * bitmap.height / image1.getHeight()
            var x = (event.x * scale).toInt()
            var y = (event.y * scale).toInt()
            if (x + 120 > bitmap.width){
                x = bitmap.width - 120
            }
            if (y + 120 > bitmap.height){
                y = bitmap.height - 120
            }
            // 显示图片的指定区域
            image2.setImageBitmap(Bitmap.createBitmap(bitmap, x, y, 120, 120))
            image2.imageAlpha = alpha
            false
        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">
        <!-- 此处省略三个按钮定义 -->
        <Button android:id="@+id/plus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="增大透明度"/>
        <Button android:id="@+id/minus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="降低透明度"/>
        <Button android:id="@+id/next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="下一张"/>
    </LinearLayout>
    <!-- 定义显示图片整体的ImageView -->
    <ImageView android:id="@+id/image1"
        android:layout_width="wrap_content"
        android:layout_height="280dp"
        android:src="@drawable/shuangta"
        android:scaleType="fitCenter"/>
    <!-- 定义显示图片局部细节的ImageView -->
    <ImageView android:id="@+id/image2"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:background="#00f"
        android:layout_margin="10dp"/>
</LinearLayout>

效果

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
最近在学习kotlin,项目中正好用到了图片浏览,就用kotlin放照微信的了一个,效果如下:大概就是这么个效果,图片从小到大的一个效果,然后滑动切换图片,点击后会返回对应图片的位置,其实比较容易,用到的是ActivityOptions,一个activity的转场动画,下面说一下怎么实现。github地址点这里关于kotlin的配置就不过多说了,网上好多教程(最近kotlin好火)布局就是一个recyclerviewmainactivity代码:可以看到就一个初始化布局管理器和设置适配器的代码没什么了(你说没看到初始化控件?看看这篇文章)到这里之前都很好理解,关键就是adapter和点击图片跳转的代码在API 21以后,我们可以使用内置的Activity切换动画。但是这样也就意味着只能兼容5.0之后的系统,ActivityOptions是一个静态类,它提供了为数不多的几种方法,我们正是用其中的makeSceneTransitionAnimation(Activity activity,Pair… sharedElements)来完成了这个效果,有兴趣的小伙伴移步至这里,先来说下传递的两个参数,第一个activity不用说,第二个pair,View是你开始动画的view对象,string是他的动画名要和结束动画的view名一致才可以,到这里应该可以理解,我们在创建适配器的时候给每个imageview起了一个transitionname当我们跳转至查看大图界面给当前的viewpage赋值相同的名称便能完成动画,然后在我们滑动时更改viewpage的transitionname为对应图片的transitionname这样在点击图片的时候就会显示对应图片的下标。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值