安卓常用控件3之图片框(ImageView)



1.xml代码的实现



<?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="com.zking.admin.android_07.MainActivity">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Button
        android:id="@+id/b_main_before"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="<"
        android:onClick="before"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="+"
        android:onClick="add"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="-"
        android:onClick="sub"/>
    <Button
        android:id="@+id/b_main_next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=">"
        android:onClick="next"/>
</LinearLayout>
    <ImageView
        android:id="@+id/iv_image_image"
        android:layout_width="350dp"
        android:layout_height="330dp"
        android:background="#ff0000"
        />
    <ImageView
        android:id="@+id/iv_image_new"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="#ff0000"
        />
    <SeekBar
        android:id="@+id/sb_main_seekbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:progress="0"
        android:max="255"
        />
 <Switch
        android:id="@+id/s_main_switch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <ToggleButton
        android:id="@+id/tb_main_toggle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>



2.java代码实现

package com.zking.admin.android_07;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.Toast;
import android.widget.ToggleButton;

import java.io.File;

public class MainActivity extends AppCompatActivity {
    private ImageView iv_image_image;
    private int images[]={R.drawable.s1,
            R.drawable.s2,
            R.drawable.s3,
            R.drawable.s4,
            R.drawable.s5
    };

    int currentIndex=0;
    int currentAlpha=255;
    private File[] files;
    private Bitmap bm;
    private ImageView iv_image_new;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv_image_image = (ImageView) findViewById(R.id.iv_image_image);
        iv_image_new = (ImageView) findViewById(R.id.iv_image_new);
       
        final SeekBar seek= (SeekBar) findViewById(R.id.sb_main_seekbar);
        seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                iv_image_image.setImageAlpha(progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
        Switch sw= (Switch) findViewById(R.id.s_main_switch);
        sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    Toast.makeText(MainActivity.this,"开",Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(MainActivity.this,"关",Toast.LENGTH_SHORT).show();
                }
            }
        });
        ToggleButton toggle= (ToggleButton) findViewById(R.id.tb_main_toggle);
        toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    Toast.makeText(MainActivity.this,"开",Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(MainActivity.this,"关",Toast.LENGTH_SHORT).show();
                }
            }
        });
        //判断 手机是否有内存卡 内存卡是否可用
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            //获取手机内存卡路径
            String sdCardPath=Environment.getExternalStorageDirectory().getAbsolutePath();
            //获取手机内存卡目录中的图片
            File file=new File(sdCardPath+"/images");
            files = file.listFiles();
        }

//        iv_image_image.setImageResource(images[currentIndex]);

        bm = BitmapFactory.decodeFile(files[0].getAbsolutePath());
        iv_image_image.setImageBitmap(bm);
        iv_image_image.setImageAlpha(currentAlpha);


        //给图片控件设置触摸事件
        iv_image_image.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                //获取触摸的坐标
                float x=motionEvent.getX();
                float y=motionEvent.getY();
                iv_image_image.setDrawingCacheEnabled(true);
                bm=iv_image_image.getDrawingCache();
                //截取
                //进行边缘判断
                //左下角
                if(y+70>bm.getHeight()){
                    y=y-70;
                }
                //右上角
                if(x+70>bm.getWidth()){
                    x=x-70;
                }
                Bitmap bmNew=Bitmap.createBitmap(bm,(int)(x),(int)(y),70,70);
                iv_image_new.setImageBitmap(bmNew);
                iv_image_image.setDrawingCacheEnabled(false);
                return true;
            }
        });
    }

    public void before(View view){
        currentIndex--;
        if(currentIndex<0){
            currentIndex=0;
            Toast.makeText(MainActivity.this, "兄弟,没得选了", Toast.LENGTH_SHORT).show();
        }
        //iv_image_image.setImageResource(images[currentIndex]);
        bm= BitmapFactory.decodeFile(files[currentIndex].getAbsolutePath());
        iv_image_image.setImageBitmap(bm);
    }
    public void next(View view){
        currentIndex++;
        if(currentIndex>=images.length){
            currentIndex=images.length-1;
            Toast.makeText(MainActivity.this, "兄弟,没得选了", Toast.LENGTH_SHORT).show();
        }
        //iv_image_image.setImageResource(images[currentIndex]);
        bm= BitmapFactory.decodeFile(files[currentIndex].getAbsolutePath());
        iv_image_image.setImageBitmap(bm);
    }

    public void add(View view){
        currentAlpha-=20;
        if(currentAlpha<=0){
            currentAlpha=0;
            Toast.makeText(MainActivity.this, "看不见了", Toast.LENGTH_SHORT).show();
        }
        iv_image_image.setImageAlpha(currentAlpha);
    }
    public void sub(View view){
        currentAlpha+=20;
        if(currentAlpha>=255){
            currentAlpha=255;
            Toast.makeText(MainActivity.this, "都给你了", Toast.LENGTH_SHORT).show();
        }
        iv_image_image.setImageAlpha(currentAlpha);
    }
    public void getxin(View view){

    }


}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值