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

来源:互联网 发布:善领p46最新数据 编辑:程序博客网 时间:2024/06/08 15:54

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



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

首先把你要展示的图片放入  项目--->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);    }}

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






原创粉丝点击