利用http的方式下载图片,并且通过seekBar控件进行图片的缩放、旋转
(一)、利用http的方式下载图片
不论我们的下载方式是通过何种方法,首先,我们还是必须拥有合理的界面设计,当然啦,这里我先给学长自己的界面设计,虽然不怎么美观,但可以帮助大家理解相应代码的名称等…
只要是通过网络连接,我们首要的就是进行网络许可的配置,如下:
<uses-permission android:name="android.permission.INTERNET" />
在android项目中的配置文件中加入以上的入网许可
1、界面设计的XML代码如下所示:
<?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"
android:orientation="vertical"
android:layout_weight="12"
tools:context=".work6_0">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="500dp"
android:layout_height="match_parent"
android:id="@+id/p1"
android:background="#C8E6C9"
android:layout_weight="1">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginBottom="5dp"
android:src="@drawable/pto2"
android:id="@+id/p"/>
</LinearLayout>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="160dp"
android:layout_toRightOf="@+id/p1"
android:id="@+id/RadioGroup"
android:layout_marginTop="5dp"
android:layout_marginBottom="10dp"
android:orientation="vertical"
android:layout_weight="1">
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="30dp"
android:text="图片一"
android:id="@+id/picture1"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="图片二"
android:id="@+id/picture2"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="图片三"
android:id="@+id/picture3"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="图片四"
android:id="@+id/picture4"/>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/suo"
android:text="缩放图片"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:id="@+id/power1"
android:progress="400"
android:min="1"
android:max="100"
android:layout_marginTop="20dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/xuan"
android:layout_weight="1"
android:text="旋转图片"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:id="@+id/power2"
android:progress="0"
android:max="360"
android:min="0"
android:layout_marginTop="20dp"/>
</LinearLayout>
</LinearLayout>
上诉界面设计代码的效果如下图显示:
2、接下来,我们需要通过http连接进行图片的下载,这里,我们首先需要在网络上找到几张通过http进行连接的图片连接,大家可以在百度上自己去找,这里学长也给出几张http连接的图片
1、http链接图片1
2、http链接图片2
3、http链接图片3
4、http链接图片4
有上述图片,避免大家早百度上自己费心寻找自己所需图片,当然,这只能用来学习,如果用来交作业的话,希望学弟学妹们还是自己多下功夫,百度问候。
3、那么我们如何利用http来进行网络上图片的下载显示的呢?所以,在你创建的页面的java文件中,我们需要一个与网络进行连接的函数(方法),代码如下:
private void setDownBitmap(final String link, final Activity activity){
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL(link);
try {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
connection.setDoInput(true);
connection.connect();
int code = connection.getResponseCode();
if (code == 200) {
InputStream inputStream = connection.getInputStream();
final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
picture.setImageBitmap(bitmap);
}
});
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}).start();
}
4、利用如上函数进行手机端与网络上的对接,那么我们在哪里调用这个函数?可以看到上面函数传递了一个url,所以,我们需要的就是哪里有url,我们就在哪里调用,我的就是上面的RadioGroup,如下所示:
imageSelect=(RadioGroup)findViewById(R.id.RadioGroup);
imageSelect.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
RadioButton radioButton=(RadioButton)findViewById(i);
String radioName=radioButton.getText().toString();
String imageLink="";
if (radioName.equals("图片一")){
imageLink="https://c-ssl.duitang.com/uploads/item/201805/25/20180525210744_8FByW.jpeg";
setDownBitmap(imageLink,work6_0.this);
}else if (radioName.equals("图片二")){
imageLink="https://c-ssl.duitang.com/uploads/item/201806/14/20180614223449_nnzcv.jpg";
setDownBitmap(imageLink,work6_0.this);
}
else if (radioName.equals("图片三")){
imageLink="https://c-ssl.duitang.com/uploads/blog/201506/06/20150606181240_s2rMJ.jpeg";
setDownBitmap(imageLink,work6_0.this);
}
else if (radioName.equals("图片四")){
imageLink="https://c-ssl.duitang.com/uploads/item/201712/03/20171203153850_NxjMi.jpeg";
setDownBitmap(imageLink,work6_0.this);
}
}
});
}
进行到这里,我们的利用http下载图片就可以实现了。希望对大家有帮助哦!
(二)、通过seekBar控件进行图片的缩放、旋转
1、通过seekBar我们需要页面开始implements一个SeekBar.OnSeekBarChangeListener,为拖动条设置一个监听
public class work6_0 extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener
2、监听里面的内容就是我们需要实现的旋转、和缩放功能,代码如下:
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b){
Matrix matrix = new Matrix();
//Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.pto2);
if (seekBar.getId() == R.id.power1) {
int newWidth = i + minWidth;
int newHeight = (int) (newWidth * 3 / 4);//按照原图片的比例缩放
picture.setLayoutParams(new LinearLayout.LayoutParams(newWidth, newHeight));
suo.setText("缩放倍数为:" + i + "%");
} else if (seekBar.getId() == R.id.power2) {
float d=i;
picture.setPivotX(picture.getWidth()/2);
picture.setPivotY(picture.getHeight()/2);//支点在图片中心
picture.setRotation(d);
xuan.setText("旋转角度为:" + i + "度");
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
通过以上代码,我们就可以实现图片的缩放功能啦!下面给大家一点关于seekBar和Matrix实现图片变化的相关知识点
利用seekBar拖动条和Matrix实现图片的变化
1、 Matrix
对于一个图片变换的处理,需要Matrix类的支持,它位于"android.graphics.Matrix"包下,是Android提供的一个矩阵工具类,它本身不能对图像或View进行变换,但它可与其他API结合来控制图形、View的变换,如Canvas。
2、Matrix提供了一些方法来控制图片变换:
setTranslate(float dx,float dy):控制Matrix进行位移。
setSkew(float kx,float ky):控制Matrix进行倾斜,kx、ky为X、Y方向上的比例。
setSkew(float kx,float ky,float px,float py):控制Matrix以px、py为轴心进行倾斜,kx、ky为X、Y方向上的倾斜比例。
setRotate(float degrees):控制Matrix进行depress角度的旋转,轴心为(0,0)。
setRotate(float degrees,float px,float py):控制Matrix进行depress角度的旋转,轴心为(px,py)。
setScale(float sx,float sy):设置Matrix进行缩放,sx、sy为X、Y方向上的缩放比例。
setScale(float sx,float sy,float px,float py):设置Matrix以(px,py)为轴心进行缩放,sx、sy为X、Y方向上的缩放比例。
关于seekBar拖动条
(三)、此项目的综合实验源代码
界面设计代码上面已经给出,这里将不给了哟,大家动动小手,往上翻一翻啦!
1、实现功能的java代码如下:
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.TextView;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class work6_0 extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener{
private ImageView picture;
private SeekBar scale1;
private SeekBar scale2;
private RadioGroup imageSelect;
private Matrix matrix;
private TextView suo;
private TextView xuan;
private int minWidth = 400;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_work6_0);
picture=(ImageView)findViewById(R.id.p);
suo=(TextView)findViewById(R.id.suo);
xuan=(TextView)findViewById(R.id.xuan);
scale1=(SeekBar)findViewById(R.id.power1);
scale2=(SeekBar)findViewById(R.id.power2);
imageSelect=(RadioGroup)findViewById(R.id.RadioGroup);
scale1.setOnSeekBarChangeListener(this);
scale2.setOnSeekBarChangeListener(this);
matrix = new Matrix();
init();
//屏幕宽度
DisplayMetrics dm=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int screen=dm.widthPixels;
//设置硕放滑动最大值
scale1.setMax(screen);
scale2.setMax(360);
imageSelect.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
RadioButton radioButton=(RadioButton)findViewById(i);
String radioName=radioButton.getText().toString();
String imageLink="";
if (radioName.equals("图片一")){
imageLink="https://c-ssl.duitang.com/uploads/item/201805/25/20180525210744_8FByW.jpeg";
setDownBitmap(imageLink,work6_0.this);
}else if (radioName.equals("图片二")){
imageLink="https://c-ssl.duitang.com/uploads/item/201806/14/20180614223449_nnzcv.jpg";
setDownBitmap(imageLink,work6_0.this);
}
else if (radioName.equals("图片三")){
imageLink="https://c-ssl.duitang.com/uploads/blog/201506/06/20150606181240_s2rMJ.jpeg";
setDownBitmap(imageLink,work6_0.this);
}
else if (radioName.equals("图片四")){
imageLink="https://c-ssl.duitang.com/uploads/item/201712/03/20171203153850_NxjMi.jpeg";
setDownBitmap(imageLink,work6_0.this);
}
}
});
}
private void setDownBitmap(final String link, final Activity activity){
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("OK");
try {
URL url = new URL(link);
try {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
connection.setDoInput(true);
connection.connect();
int code = connection.getResponseCode();
if (code == 200) {
InputStream inputStream = connection.getInputStream();
final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
picture.setImageBitmap(bitmap);
}
});
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}).start();
}
public void init(){
picture.setImageResource(R.drawable.pto2);
}
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b){
Matrix matrix = new Matrix();
//Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.pto2);
if (seekBar.getId() == R.id.power1) {
int newWidth = i + minWidth;
int newHeight = (int) (newWidth * 3 / 4);//按照原图片的比例缩放
picture.setLayoutParams(new LinearLayout.LayoutParams(newWidth, newHeight));
suo.setText("缩放倍数为:" + i + "%");
} else if (seekBar.getId() == R.id.power2) {
float d=i;
picture.setPivotX(picture.getWidth()/2);
picture.setPivotY(picture.getHeight()/2);//支点在图片中心
picture.setRotation(d);
xuan.setText("旋转角度为:" + i + "度");
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
以上就是本次博客的内容啦,希望对学弟学妹们有帮助,欢迎大家留言评论哦!