常用控件(三)
来源:互联网 发布:xp怎么连接网络打印机 编辑:程序博客网 时间:2024/06/07 04:05
进度条和抠图以及获取手机内存卡的图片
先写3个xml文件
1.
<?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: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: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="delete" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=">" android:onClick="next" /> </LinearLayout> <ImageView android:layout_width="300dp" android:layout_height="400dp" android:id="@+id/iv_girl_image" android:background="#ffff00" /> <ImageView android:layout_width="60dp" android:layout_height="60dp" android:id="@+id/iv_girl_new" /></LinearLayout>
2.
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.zking.g160618_android06_widget.MainActivity"> <ImageView android:layout_width="60dp" android:layout_height="60dp" android:background="#00ff00" android:scaleType="centerInside" android:src="@drawable/largeimage" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/largeimage" /></LinearLayout>
3.
<?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"> <ProgressBar style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/progressBar" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv_progress_num" android:textSize="30sp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="start" android:text="开始" /></LinearLayout>
下面是java代码
首先是抠图,就是图片局部放大
package com.zking.g160618_android06_widget;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;import android.os.Environment;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.MotionEvent;import android.view.View;import android.widget.ImageView;import android.widget.Toast;import java.io.File;/** * @author Zking-Snail * @time 2017/6/9 11:40 * @Version ${REV} */public class GirlActivity extends AppCompatActivity{ //定义图片数组 private int images[]={R.drawable.s1,R.drawable.s2,R.drawable.s3,R.drawable.s4,R.drawable.s5}; private ImageView iv_girl_image; private int currentIndex=0; private int currentAlpha=255; private File[] files; private ImageView iv_girl_new; private Bitmap bitmap; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_girl); iv_girl_image = (ImageView) findViewById(R.id.iv_girl_image); iv_girl_new = (ImageView) findViewById(R.id.iv_girl_new); //获取手机内存卡中的图片 if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ //获取内存卡的路径 String sdcardPath=Environment.getExternalStorageDirectory().getAbsolutePath(); File file=new File(sdcardPath+"/images"); files = file.listFiles(); } //通过代码指定第一张默认图片 //iv_girl_image.setImageResource(images[currentIndex]); bitmap = BitmapFactory.decodeFile(files[currentIndex].getAbsolutePath()); iv_girl_image.setImageBitmap(bitmap); //设置透明度 iv_girl_image.setImageAlpha(currentAlpha); //给ImageView设置触摸事件 iv_girl_image.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { //获取手指的坐标 float x=motionEvent.getX(); float y=motionEvent.getY(); //抠图 Bitmap newBitmap=Bitmap.createBitmap(bitmap,(int)x,(int)y,30,30); iv_girl_new.setImageBitmap(newBitmap); return true; } }); } public void before(View view){ currentIndex--; //Log.i("test","当前值:"+currentIndex); if(currentIndex<0){ currentIndex=0; Toast.makeText(GirlActivity.this, "兄弟,乖乖充会员吧!", Toast.LENGTH_SHORT).show(); } //iv_girl_image.setImageResource(images[currentIndex]); bitmap=BitmapFactory.decodeFile(files[currentIndex].getAbsolutePath()); iv_girl_image.setImageBitmap(bitmap); } public void next(View view){ currentIndex++; //Log.i("test","当前值:"+currentIndex); if(currentIndex>=files.length){ currentIndex=files.length-1; Toast.makeText(GirlActivity.this, "兄弟,请冲会员,查看更多", Toast.LENGTH_SHORT).show(); } //iv_girl_image.setImageResource(images[currentIndex]); bitmap=BitmapFactory.decodeFile(files[currentIndex].getAbsolutePath()); iv_girl_image.setImageBitmap(bitmap); } public void add(View view){ currentAlpha+=20; if(currentAlpha>255){ currentAlpha=255; Toast.makeText(GirlActivity.this, "兄弟,很高清了", Toast.LENGTH_SHORT).show(); } iv_girl_image.setImageAlpha(currentAlpha); } public void delete(View view){ currentAlpha-=20; if(currentAlpha<0){ currentAlpha=0; Toast.makeText(GirlActivity.this, "兄弟,没得看了", Toast.LENGTH_SHORT).show(); } iv_girl_image.setImageAlpha(currentAlpha); }}
获取手机内存卡的路径还需要在 AndroidManifest.xml写代码
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.a_02.a_06"> <!--获取内存卡的权限--> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> </activity> <activity android:name=".test"> </activity> <activity android:name=".work"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
然后到进度条了
package com.zking.g160618_android06_widget;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.ProgressBar;import android.widget.TextView;/** * @author Zking-Snail * @time 2017/6/11 11:11 * @Version ${REV} */public class ProgressActivity extends AppCompatActivity { private ProgressBar progressBar; private TextView tv_progress_num; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_progress); progressBar = (ProgressBar) findViewById(R.id.progressBar); tv_progress_num = (TextView) findViewById(R.id.tv_progress_num); } //ANR :application not responsing应用程序无响应 //why:因为在主线程中执行了 耗时的操作 //how:把耗时的操作 写到子线程中 public void start(View view){ new MyThread().start(); } Handler handler=new Handler(){ //接受消息,并更新UI @Override public void handleMessage(Message msg) { super.handleMessage(msg); //接受消息 int i=msg.what;// 更新UI tv_progress_num.setText(i+"%"); } }; class MyThread extends Thread{ @Override public void run() { super.run(); for (int i = 0; i <=100 ; i++) { progressBar.setProgress(i); //发消息 handler.sendEmptyMessage(i); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } }}
这里要
注意耗时的事件不要写道主线程中,会导致程序无响应
阅读全文
0 0
- 常用控件(三)
- Android 常用 控件(三)
- Android常用控件(三)
- Android 常用控件(三)
- UIVIew进阶-常用控件(三):UIImageView
- Android 常用控件(三)学习笔记
- OC常用控件三
- Anroid 常用控件(三)
- android常用控件三 AutoCompleteTextView
- 常用控件(三) : 图片轮播控件
- android常用控件(三)- ProgressBar、ListView
- Android 常用系统控件界面大合集(三)
- Android学习第13课—常用控件(三)
- Windows phone8 基础篇(三) 常用控件开发
- Android常用控件(三)- ProgressBar、ListView
- android快速上手(三)常用控件使用
- 黑马程序员-自己总结的winform常用控件(三)
- IOS基本控件常用属性及方法(三)---UIImageView
- Scala基础—集合函数式编程示例(占位符的使用示例)
- redis scan 命令遇到的坑总结
- CPU卡COS系统文件结构详解
- mvc 安装和卸载 bootstrap
- Mac brew 安装 php7 一些注意事项
- 常用控件(三)
- Java组合设计模式
- hdoj 2111
- 考研之华东师范
- Educational Codeforces Round 23#B. Makes And The Product
- shiro的freemaker标签配置使用
- 随机投影森林-一种近似最近邻方法(ANN)
- 解救小Q UESTC
- YII2 同时使用RESTful和普通控制器