android : SeekBar 实现图片旋转缩放
Android 使用 SeekBar 控件实现图片旋转缩放。
图片来源网络地址。
使用工具
- android studio (ver. 3.5.1)
- android(sdk 29)
- java(ver.1.8.0)
- gradle(ver. 5.4.1)
功能实现
由于使用的图片是网络图片,所以先在 AndroidManifest.xml 添加网络权限:
<uses-permission android:name="android.permission.INTERNET"/>
在 onCreate() 创建一个 handler来接收子线程的消息更新下载的图片
handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case DOWNLOAD_CODE:
/*
* 更新UI
* 提取消息中的bitmap,并设置ImageView
* */
Bitmap bitmap=(Bitmap) msg.obj;
if (bitmap!=null){
imageView.setImageBitmap(bitmap);//disPlay image
}
break;
case DOWNLOAD_FAIL:
Toast.makeText(MainActivity.this,"下载失败",Toast.LENGTH_SHORT).show();
break;
}
}
自定义GetPictThread类实现Runnable类
public class GetPictThread implements Runnable{
//定义handler和path
public Handler handler;
public String path;
//带参构造
public GetPictThread(Handler handler, String path) {
this.handler = handler;
this.path = path;
}
//在run方法中实现图片下载
@Override
public void run() {
//通过Get方法请求获取网络图片
try {
URL url=new URL(path);
HttpURLConnection connection=(HttpURLConnection) url.openConnection();
//设置请求方式
connection.setRequestMethod("GET");
//设置超时时间
connection.setConnectTimeout(30*1000);
//发起连接
connection.connect();
//获取状态码
int requestCode=connection.getResponseCode();
System.out.println(requestCode);
if (requestCode==HttpURLConnection.HTTP_OK){
/*
* 1.获得文件长度
* 2.通过缓冲输入流
* 3.将输入流转换成字节数组
* 4.将字节数组转换成位图
* */
fileLength=connection.getContentLength();
InputStream is=new BufferedInputStream(connection.getInputStream());
//获取到字节数组
byte[] arr=streamToArr(is);
//将字节数组转换成位图
mBitmap= BitmapFactory.decodeByteArray(arr,0,arr.length);
/*
* 下载完成后将消息发送出去
* 通知主线程,更新UI
* */
Message message=Message.obtain();
message.what=DOWNLOAD_CODE;
message.obj=mBitmap;
handler.sendMessage(message);
}else {
Log.e("TAG", "run:error "+requestCode);
}
}catch (MalformedURLException e){
e.printStackTrace();
handler.sendEmptyMessage(DOWNLOAD_FAIL);
}catch (IOException e){
e.printStackTrace();
handler.sendEmptyMessage(DOWNLOAD_FAIL);
}
}
}
将输入流转换成字节数组
public byte[] streamToArr(InputStream inputStream){
try {
ByteArrayOutputStream baos=new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
int len;
while ((len=inputStream.read(buffer))!=-1){
baos.write(buffer,0,len);
}
//关闭输出流
baos.close();
//关闭输入流
inputStream.close();
//返回字节数组
return baos.toByteArray();
}catch (IOException e){
e.printStackTrace();
//若失败,则返回空
return null;
}
}
监听图片放大的 SeekBar
class seekBar1ChangeListenner implements SeekBar.OnSeekBarChangeListener {
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
int newWidth = progress + minWidth;
int newHeight = (int) (newWidth * 3 / 4);//按照原图片的比例缩放
imageView.setLayoutParams(new LinearLayout.LayoutParams(
newWidth, newHeight));
}
监听图片旋转的 SeekBar
class seekBar2ChangeListenner implements OnSeekBarChangeListener{
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
//Bitmap bitmap = ((BitmapDrawable) (getResources().getDrawable(R.drawable.p))).getBitmap();
Bitmap bitmap=mBitmap;
matrix.setRotate(progress);//设置翻转的角度
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth() , bitmap.getHeight(), matrix,true);
imageView.setImageBitmap(bitmap);
textView2.setText(progress+"度");
}
}