使用seekbar&http下载图片并进行缩放旋转
作者:互联网
使用seekbar&http下载图片并进行缩放旋转
.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=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下载图片"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myImage"
android:layout_gravity="center"
android:maxHeight="500dp"
android:maxWidth="500dp"
android:adjustViewBounds="true"
android:src="@mipmap/pic2"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/imageSelect">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="图片1"
android:layout_weight="1"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="图片2"
android:layout_weight="1"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="图片3"
android:layout_weight="1"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="图片4"
android:layout_weight="1"/>
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="图片缩放"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/sb_scale"
android:progress="100"
android:layout_marginTop="20dp"
android:min="1"
android:max="100"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="图片旋转"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/sb_roaste"
android:layout_marginTop="20dp"
android:progress="0"
android:min="0"
android:max="360"/>
</LinearLayout>
布局非常好看懂
MainActivity文件
private ImageView myImage;
private RadioGroup imageselect;
private SeekBar sb_scale;
private SeekBar sb_roate;
private Bitmap downloadbitmap;
private Bitmap mbitmap;
int k0,k1,k;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageselect = findViewById(R.id.imageSelect);
myImage = findViewById(R.id.myImage);
sb_scale = findViewById(R.id.sb_scale);
sb_roate = findViewById(R.id.sb_roaste);
sb_scale.setMax(100);
sb_roate.setMax(360);
k=k0=k1=0;
以上定义了相关布局并进行初始化。其中定义了三个K,进行后面的判断。
以能达到使图片伸缩,旋转。并且对下载后的图片进行伸缩旋转、及对伸缩后的图片进行旋转
imageselect.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton radioButton = findViewById(checkedId);
String radioName = radioButton.getText().toString();
String imageLink = "";
if (radioName.equals("图片1")){
imageLink= "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcdn.duitang.com%2Fuploads%2Fitem%2F201410%2F04%2F20141004172507_J8Mty.jpeg&refer=http%3A%2F%2Fcdn.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1625910844&t=868a2ed6ec1b60dcb1761558c608d65f";
setDownBitmap(imageLink,MainActivity.this,myImage);
k1 = 1;
}else if (radioName.equals("图片2")){
imageLink = "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3228549874,2173006364&fm=26&gp=0.jpg";
setDownBitmap(imageLink,MainActivity.this,myImage);
k1 = 1;
}else if (radioName.equals("图片3")){
imageLink = "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2496571732,442429806&fm=26&gp=0.jpg";
setDownBitmap(imageLink,MainActivity.this,myImage);
k1 = 1;
}else if (radioName.equals("图片4")){
imageLink = "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3870964477,3746012709&fm=26&gp=0.jpg";
setDownBitmap(imageLink,MainActivity.this,myImage);
k1 = 1;
}
}
});
一个RadioGroup,进行对下载图片的选择
public void setDownBitmap(final String link,final Activity activity,final ImageView imageView){
new Thread(){
public void run(){
try {
URL url = new URL(link);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream inputStream = connection.getInputStream();
final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
downloadbitmap = bitmap;
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
imageView.setImageBitmap(bitmap);
}
});
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
下载图片使用http,创建一个子进程进行图片下载,将下载的图片转为bitmap。同时在其中使用了一个定义的downloadbitmap来进行下载图片的保存,将其放入到ImageView中。使用了http下载图片,因此需要在Manifest.xml中加入权限
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
可能不需要加完,加第一个好像就可以。加着不碍事~
sb_scale.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
Matrix matrix = new Matrix();
if (k1 != 0){
Bitmap bitmapscale = downloadbitmap;
float scaleX = (float) progress/100;
float scaleY = (float) progress/100;
matrix.postScale(scaleX,scaleY);
Bitmap bitmap1 = Bitmap.createBitmap(bitmapscale,0,0,bitmapscale.getWidth(),bitmapscale.getHeight(),matrix,true);
myImage.setImageBitmap(bitmap1);
k=1;
mbitmap =bitmap1;
}else if (k0 == 0 &&k1 ==0){
Bitmap bitmapscale = BitmapFactory.decodeResource(getResources(),R.mipmap.pic2);
float scaleX = (float) progress/100;
float scaleY = (float) progress/100;
matrix.postScale(scaleX,scaleY);
Bitmap bitmap1 = Bitmap.createBitmap(bitmapscale,0,0,bitmapscale.getWidth(),bitmapscale.getHeight(),matrix,true);
myImage.setImageBitmap(bitmap1);
k=1;
mbitmap =bitmap1;
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
sb_roate.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
Matrix matrix =new Matrix();
if (k1 != 0){
if (k==1){
float d =progress;
matrix.setRotate(d);
Bitmap bitmap1 = Bitmap.createBitmap(mbitmap,0,0,mbitmap.getWidth(),mbitmap.getHeight(),matrix,true);
myImage.setImageBitmap(bitmap1);
}else {
Bitmap bitmaproate = downloadbitmap;
float d =progress;
matrix.setRotate(d);
Bitmap bitmap1 = Bitmap.createBitmap(bitmaproate,0,0,bitmaproate.getWidth(),bitmaproate.getHeight(),matrix,true);
myImage.setImageBitmap(bitmap1);
}
}else if (k1 ==0 &k0 == 0){
if (k == 1) {
float d =progress;
matrix.setRotate(d);
Bitmap bitmap1 = Bitmap.createBitmap(mbitmap,0,0,mbitmap.getWidth(),mbitmap.getHeight(),matrix,true);
myImage.setImageBitmap(bitmap1);
}else {
Bitmap bitmaproate = BitmapFactory.decodeResource(getResources(),R.mipmap.pic2);
float d =progress;
matrix.setRotate(d);
Bitmap bitmap1 = Bitmap.createBitmap(bitmaproate,0,0,bitmaproate.getWidth(),bitmaproate.getHeight(),matrix,true);
myImage.setImageBitmap(bitmap1);
}
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
两个seekbar的监听。其中使用了if判断,是否为下载的图片,对下载图片进行seekbar控制缩放及旋转。是否为已经缩放的图片,对其再进行旋转。
在seekbar监听中,每动一下seekbar就会执行监听。因此,在逻辑上可以慢慢推
效果
Android-studio模拟机演示
标签:http,matrix,缩放,seekbar,void,float,bitmap1,Bitmap,myImage 来源: https://blog.csdn.net/TBMmagic/article/details/117789080