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="<" 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=">" 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); }}
希望这个案例对你有所帮助,请多多指教!
阅读全文
0 0
- android常用控件三(图片框ImageView, SD文件读取)
- Android常用控件六之图片框(ImageView)
- 安卓常用控件3之图片框(ImageView)
- 卓常用控件3之图片框(ImageView)
- Android 图片控件ImageView
- Android常用控件(ImageView)
- android 常用控件--ImageView
- Android ImageView图片浏览器(ImageView加载sd卡图片资源)的内存溢出问题分析
- Android ImageView图片浏览器(ImageView加载sd卡图片资源)的内存溢出问题分析
- android 常用控件之 (ImageView) 使用
- android 控件 图片视图(ImageView)
- 从SD卡上读取图片放置在ImageView上
- Android常用控件之ImageView
- Android常用控件之ImageView
- Android 控件之四:ImageView 图片框,ImageButton 图片按钮
- android 读取SD卡文件
- android sd卡文件读取
- android 读取SD卡文件
- centos7.2 安装Zabbix3.2.6 监控系统安装
- JSP —— web.xml 中security-constraint 和 4 种认证类型
- 比特率 波特率 数据传输速率 区别
- linux debian8.2系统安装mysql
- POJ3308 Paratroopers
- android常用控件三(图片框ImageView, SD文件读取)
- mc.exe在VS上的配置方式
- Akka并发编程——第四节:Actor模型(三)
- 去除重复字符串
- Hadoop相关概念
- HDU
- 【剑指offer】面试题 33:二叉搜索树的后序遍历序列
- Codeforecs XOR On Segment 线段树(区间异或,求和)
- linux debian系统的两台主机之间建立安全信任关系及scp传输文件的免密操作