forked from hzuapps/android-labs-2019
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
df2b711
commit 9e09954
Showing
4 changed files
with
187 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...5/app/src/main/java/edu/hzuapps/androidlabs/soft1714080902205/getimage/HeadImageView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package edu.hzuapps.androidlabs.soft1714080902205.getimage; | ||
|
||
import android.content.Context; | ||
import android.graphics.Bitmap; | ||
import android.graphics.BitmapFactory; | ||
import android.os.Handler; | ||
import android.os.Message; | ||
import android.util.AttributeSet; | ||
import android.widget.Toast; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
|
||
public class HeadImageView extends android.support.v7.widget.AppCompatImageView { | ||
public static final int GET_DATA_SUCCESS = 1; | ||
public static final int NETWORK_ERROR = 2; | ||
public static final int SERVER_ERROR = 3; | ||
//子线程不能操作UI,通过Handler设置图片 | ||
private Handler handler = new Handler() { | ||
@Override | ||
public void handleMessage(Message msg) { | ||
switch (msg.what){ | ||
case GET_DATA_SUCCESS: | ||
Bitmap bitmap = (Bitmap) msg.obj; | ||
setImageBitmap(bitmap); | ||
break; | ||
case NETWORK_ERROR: | ||
Toast.makeText(getContext(),"网络连接请求失败",Toast.LENGTH_SHORT).show(); | ||
break; | ||
case SERVER_ERROR: | ||
Toast.makeText(getContext(),"服务器错误", Toast.LENGTH_SHORT).show(); | ||
break; | ||
} | ||
} | ||
}; | ||
|
||
public HeadImageView(Context context, AttributeSet attrs, int defStyleAttr) { | ||
super(context, attrs, defStyleAttr); | ||
} | ||
|
||
public HeadImageView(Context context) { | ||
super(context); | ||
} | ||
|
||
public HeadImageView(Context context, AttributeSet attrs) { | ||
super(context, attrs); | ||
} | ||
|
||
//设置网络图片 | ||
public void setImageURL(final String path) { | ||
//开启一个线程用于联网 | ||
new Thread() { | ||
@Override | ||
public void run() { | ||
try { | ||
//把传过来的路径转成URL | ||
URL url = new URL(path); | ||
//获取连接 | ||
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | ||
//使用GET方法访问网络 | ||
connection.setRequestMethod("GET"); | ||
//超时时间为10秒 | ||
connection.setConnectTimeout(10000); | ||
//获取返回码 | ||
int code = connection.getResponseCode(); | ||
if (code == 200) { | ||
InputStream inputStream = connection.getInputStream(); | ||
//使用工厂把网络的输入流生产Bitmap | ||
Bitmap bitmap = BitmapFactory.decodeStream(inputStream); | ||
//利用Message把图片发给Handler | ||
Message msg = Message.obtain(); | ||
msg.obj = bitmap; | ||
msg.what = GET_DATA_SUCCESS; | ||
handler.sendMessage(msg); | ||
inputStream.close(); | ||
}else { | ||
//服务启发生错误 | ||
handler.sendEmptyMessage(SERVER_ERROR); | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
//网络连接错误 | ||
handler.sendEmptyMessage(NETWORK_ERROR); | ||
} | ||
} | ||
}.start(); | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
...02205/app/src/main/java/edu/hzuapps/androidlabs/soft1714080902205/getimage/ImageShow.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package edu.hzuapps.androidlabs.soft1714080902205.getimage; | ||
|
||
import android.support.v7.app.AppCompatActivity; | ||
import android.os.Bundle; | ||
import android.view.View; | ||
import android.widget.Button; | ||
import android.widget.EditText; | ||
|
||
import edu.hzuapps.androidlabs.soft1714080902205.R; | ||
|
||
public class ImageShow extends AppCompatActivity { | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.image_show_activity); | ||
|
||
final HeadImageView headImageView = (HeadImageView) findViewById(R.id.image_view); | ||
final EditText et_image = (EditText) findViewById(R.id.et_image) ; | ||
Button button = (Button) findViewById(R.id.button_image); | ||
button.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View v) { | ||
//直接把网络的图片路径写上就可以显示网络的图片了 | ||
headImageView.setImageURL(et_image.getText().toString().trim()); | ||
} | ||
}); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
students/soft1714080902205/app/src/main/res/layout/image_show_activity.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<RelativeLayout | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:orientation="vertical"> | ||
|
||
<TextView | ||
android:id="@+id/tv_img_title" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:text="设置书籍封面:" | ||
android:textSize="30dp" | ||
|
||
/> | ||
|
||
<TextView | ||
android:id="@+id/tv_image" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="图片网址" | ||
android:textSize="20dp" | ||
android:layout_below="@id/tv_img_title" | ||
/> | ||
|
||
<EditText | ||
android:id="@+id/et_image" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:ems="250" | ||
android:layout_toRightOf="@id/tv_image" | ||
android:text="@string/url_text" | ||
android:layout_below="@id/tv_img_title" | ||
/> | ||
|
||
<Button | ||
android:id="@+id/button_image" | ||
android:text="加载网络图片" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:textSize="20sp" | ||
android:layout_below="@id/et_image" | ||
/> | ||
|
||
|
||
|
||
<edu.hzuapps.androidlabs.soft1714080902205.getimage.HeadImageView | ||
android:id="@+id/image_view" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:layout_below="@id/button_image"/> | ||
|
||
</RelativeLayout> |