android常用控件三(图片框ImageView, SD文件读取)

本文介绍了一个Android应用案例,展示了如何使用ImageView控件实现图片切换及通过加减按钮调节图片清晰度的功能。操作步骤包括将图片放入项目资源文件夹,并设置XML布局。案例旨在为开发者提供参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天给大家分享一个关于图片的案例,先来张效果:



该效果是如何实现的呢?下面我给大家分享一下,“+  , -”是加,减清晰度,“<    >”是上一张图片和下一张图片,

首先把你要展示的图片放入  项目--->res --->drawable中,在xml中把布局弄好,

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="&lt;"
        android:textSize="20dp"
        android:id="@+id/bu_main_par"
        android:onClick="per"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="+"
        android:textSize="20dp"
        android:onClick="add"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="-"
        android:textSize="20dp"
        android:onClick="sub"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="&gt;"
        android:textSize="20dp"
        android:onClick="next"
        />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
  >
   <ImageView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/iv_main_image"
       android:background="#55ff0000"

       />

    <ImageView
        android:id="@+id/iv_main_newimage"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#55000000" />


</LinearLayout>

然后再写java代码,代码有详细解析

package com.example.mylianxi01;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private ImageView iv_main_image;
    private int image [] ={R.drawable.s1,R.drawable.s2,R.drawable.s3,R.drawable.s4};
    private int currentIndex=0;
    private ImageView iv_main_newimage;
    private int currentAlpta=255;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv_main_image = (ImageView) findViewById(R.id.iv_main_image);
        iv_main_newimage = (ImageView) findViewById(R.id.iv_main_newimage);
       //设置默认图片
        iv_main_image.setImageResource(image[0]);
        //设置图片默认清晰度
        iv_main_image.setImageAlpha(currentAlpta);

       //设置触摸事件
        iv_main_image.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
               //设置触摸坐标
                float x= motionEvent.getX();
                float y= motionEvent.getY();
                Resources rs= getResources();
                Bitmap bit= BitmapFactory.decodeResource(rs,image[currentIndex]);
                //抠图
                Bitmap bitmapNew=bit.createBitmap(bit,(int)x,(int)y,50,50);
                iv_main_newimage.setImageBitmap(bitmapNew);
                return true;

            }
        });
         

    }

   //上一张图片
    public void per(View view){
        currentIndex--;
        if(currentIndex<0){
            currentIndex=0;
            Toast.makeText(this, "小伙子,第一张图了,别点了", Toast.LENGTH_SHORT).show();
        }
        //设置当前展示图片
        iv_main_image.setImageResource(image[currentIndex]);

    }
  
    //下一张图片
    public void next(View view){
        currentIndex++;
        //判断图片长度,当下一张图片长度大于数组图片最大长度时,设置为最后一张图片
        if(currentIndex>=image.length){
            currentIndex=image.length-1;
            Toast.makeText(this, "小伙子,最后一张图了,别点了", Toast.LENGTH_SHORT).show();
        }
        //设置当前展示图片
iv_main_image.setImageResource(image[currentIndex]); }
    //加清晰度
    public void add(View view){
       currentAlpta+=20;
        //判断清晰度,清晰度最大为255
        if(currentAlpta>=255){
            currentAlpta=255;
        }
        iv_main_image.setImageAlpha(currentAlpta);

    }

    //减清晰度
    public void sub(View view){
        currentAlpta-=20;
        if(currentAlpta<=0){
            currentAlpta=0;
        }iv_main_image.setImageAlpha(currentAlpta);
    }


}

希望这个案例对你有所帮助,请多多指教!






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值