标签:图书 art over font pac match pat request cto
<uses-permission android:name="android.permission.INTERNET" />
和注册Introduction.class文件<activity android:name=".Introduction"></activity>
<?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>
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); } }); } }
<?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; /** * Created by pj on 2017/3/28. */ public class Introduction extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.introduction); Button btn_r = (Button)findViewById(R.id.btn_return); btn_r.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Introduction.this,MainActivity.class); startActivity(intent); } }); } }
标签:图书 art over font pac match pat request cto
原文地址:http://www.cnblogs.com/m1063818045/p/6637603.html