目录
一、界面布局
1、界面展示
如示界面
2、布局代码
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv_pic"
android:layout_width="150dp"
android:layout_height="300dp"
android:scaleType="center"
android:src="@drawable/q1"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/radioGroup">
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="图片1"
android:layout_marginTop="40dp"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="图片2"
android:layout_marginTop="10dp"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="图片3"
android:layout_marginTop="10dp"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="图片4"
android:layout_marginTop="10dp"/>
</RadioGroup>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="缩放图片"
android:textColor="#ff3333"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/sb_scale"
android:layout_marginTop="20dp"
android:progress="10"
android:max="50"
android:min="1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="旋转图片"
android:textColor="#ff3333"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/sb_roate"
android:layout_marginTop="20dp"/>
二、功能实现
按钮以及相关定义
private int pFlag1, pFlag2;
Bitmap bitmap;
private ImageView iv_pic;
private SeekBar sb_scale;
private SeekBar sb_roate;
RadioGroup radioGroup;
private Matrix matrix;
设置监听以及点击事件
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_pic = (ImageView) findViewById(R.id.iv_pic);
sb_scale = (SeekBar) findViewById(R.id.sb_scale);
sb_roate = (SeekBar) findViewById(R.id.sb_roate);
radioGroup=(RadioGroup)findViewById(R.id.radioGroup);
sb_scale.setOnSeekBarChangeListener(this);
sb_roate.setOnSeekBarChangeListener(this);
radioGroup.setOnCheckedChangeListener(this);
pFlag1 = pFlag2 = 0;
}
图片的缩放旋转
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
Matrix matrix = new Matrix();
if (pFlag2 != pFlag1) {
pFlag1 = pFlag2;
bitmap = ((BitmapDrawable) iv_pic.getDrawable()).getBitmap();
} else if (pFlag1 == 0 && pFlag2 == 0) {
sb_scale.setProgress(10);
sb_roate.setProgress(0);
return;
} else ;
if (seekBar.getId() == R.id.sb_scale) {
float scaleX = (float) i / 10;
float scaleY = (float) i / 10;
matrix.postScale(scaleX, scaleY);
} else {
float d = i;
matrix.setRotate(d);
}
Bitmap bitmap1 = bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
iv_pic.setImageBitmap(bitmap1);
}
下载图片并显示在imageview
public void onCheckedChanged(RadioGroup radioGroup, int i) {
RadioButton radioButton = (RadioButton) findViewById(i);
String radioName = radioButton.getText().toString();
String imageLink = " ";
if (radioName.equals("图片1")) {
imageLink = "https://i.postimg.cc/yxCMcyHJ/q2.jpg";
pFlag2 = 1;
} else if (radioName.equals("图片2")) {
imageLink = "https://i.postimg.cc/KcLkbGpQ/q3.jpg";
pFlag2 = 2;
} else if (radioName.equals("图片3")) {
imageLink = "https://i.postimg.cc/MGNHkNyh/q4.jpg";
pFlag2 = 3;
} else if (radioName.equals("图片4")) {
imageLink = "https://i.postimg.cc/8P4CkjRd/q5.jpg";
pFlag2 = 4;
}
setPicBitmap(imageLink, MainActivity.this, iv_pic);
}
子线程
public static void setPicBitmap(final String url, final Activity activity, final ImageView imageView) {
new Thread() {
@Override
public void run() {
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.connect();
InputStream iStream = connection.getInputStream();
final Bitmap bitmap = BitmapFactory.decodeStream(iStream);
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
imageView.setImageBitmap(bitmap);
}
});
iStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
授予网络权限
<uses-permission android:name="android.permission.INTERNET" />
三、效果演示
选择图片2
缩放
旋转