1.编写目的:用于用户查看图书信息。
2.情景设计:本产品用于小说阅读软件。随着电子产品的普及,大部分的人群选择使用电子书,为了方便用户更清楚的了解图书的信息,明智的选择适合自己的图书而开发。
1.首先创建一个项目,然后在新建一个introduction.xml文件和Introduction.class文件,最后AndroidMainfest.xml文件中加入允许访问网络的指令<uses-permission android:name="android.permission.INTERNET" />
和注册Introduction.class文件<activity android:name=".Introduction"></activity>
2.在activity_main.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:gravity="center"
tools:context="com.example.pj.download.MainActivity">
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_picture"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="图片下载"
android:textSize="30sp"
android:gravity="center"/>
<Button
android:id="@+id/btn_introduction"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="图书简介"
android:textSize="30sp"
android:gravity="center"/>
</LinearLayout>
3.在MainActivity.class文件中写入功能
package com.example.pj.download;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
private Button btn_p;
private Button btn_i;
private ImageView img;
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
img.setImageBitmap((Bitmap) msg.obj);
}
};
public Bitmap getInternetPicture(String UrlPath) {
HttpURLConnection httpURLConnection = null;
Bitmap bm = null;
try {
URL url = new URL(UrlPath);
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setReadTimeout(5000);
httpURLConnection.setConnectTimeout(10000);
int responseCode = httpURLConnection.getResponseCode();
if (responseCode == 200) {
InputStream is = httpURLConnection.getInputStream();
bm = BitmapFactory.decodeStream(is);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (httpURLConnection != null) {
httpURLConnection.disconnect();
}
}
return bm;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView) findViewById(R.id.img);
btn_p = (Button) findViewById(R.id.btn_picture);
btn_i = (Button) findViewById(R.id.btn_introduction);
btn_p.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Thread(new Runnable() {
@Override
public void run() {
String URL = "https://img3.doubanio.com/lpic/s1076932.jpg";
Bitmap bm = getInternetPicture(URL);
Message msg = new Message();
msg.obj = bm;
handler.sendMessage(msg);
}
}).start();
}
});
btn_i.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,Introduction.class);
startActivity(intent);
}
});
}
}
4.在introduction.xml文件中加入控件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="
<Button
android:id="@+id/btn_return"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="返回"
android:layout_marginTop="20dp"/>
</LinearLayout>
package com.example.pj.download;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;