public class MainActivity extends Activity {
Button btn1, btn2, btn3;
ImageView imv;
LinearLayout linear;
Bitmap[] bitmage = new Bitmap[2];
int i;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) this.findViewById(R.id.button1);
btn2 = (Button) this.findViewById(R.id.button2);
btn3 = (Button) this.findViewById(R.id.button3);
imv = (ImageView) this.findViewById(R.id.imageView1);
linear = (LinearLayout) this.findViewById(R.id.linear);
// !4.0以上要寫這段允許執行序網路下載----------------------------------------------------
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
// !-----------------------------------------------------
linear.setVisibility(View.INVISIBLE);
//一進來設為隱藏
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
linear.setVisibility(View.VISIBLE);
//按下去,下載中會跑出來
imv.setVisibility(View.INVISIBLE);
//圖片設為隱藏
imv.postDelayed(new Runnable() {
@Override
public void run() {
String path = "";
path = "http://chart.googleapis.com/chart?"
+ "chxs=0,000000,11.5&"
+ "chxt=x&chs=300x150&cht=p&chco=0000FF|FF0000|008000|990066&"
+ "chd=t:51.8,34.3,8.1,5.9&"
+ "chl=Android|iOS|RIM|Other&"
+ "chtt=Source%3A+Nielsen+(Juky+2012)";
bitmage[0] = getBitmapFromURL(path);
imv.setImageBitmap(bitmage[0]);
linear.setVisibility(View.INVISIBLE);
//下載完.設為隱藏
imv.setVisibility(View.VISIBLE);
//下載完顯示出來
}
}, 5000);
}
});
// !---------------------------------------------------------------
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
linear.setVisibility(View.VISIBLE);
//按下去,下載中會跑出來
imv.setVisibility(View.INVISIBLE);
//圖片設為隱藏
imv.postDelayed(new Runnable() {
@Override
public void run() {
String path;
path = "http://chart.googleapis.com/chart"
+ "?chs=400x200" + "&cht=map:auto"
+ "&chco=B3BCC0|990066" + "&chld=TW"
+ "&chdl=Taiwan" + "&chm=s,FF0000,0,-1,5";
bitmage[1] = getBitmapFromURL(path);
imv.setImageBitmap(bitmage[1]);
linear.setVisibility(View.INVISIBLE);
//下載完.設為隱藏
imv.setVisibility(View.VISIBLE);
//下載完顯示出來
}
}, 1000);
}
});
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// !下載的兩張圖輪流撥放-----------------------------------
imv.postDelayed(new Runnable() {
@Override
public void run() {
imv.setImageBitmap(bitmage[i % bitmage.length]);
i++;
imv.postDelayed(this, 2000);
setTitle("播放中.....");
}
}, 2000);
}
});
}
public Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
xml:
<RelativeLayout
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"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="下載圓餅圖1" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="下載台灣分佈圖" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="兩張圖輪流撥放" />
</LinearLayout>
<LinearLayout
android:id="@+id/linear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/linearLayout1"
android:orientation="vertical" >
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下載中..."
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/linear"
android:layout_marginLeft="18dp"
android:layout_marginTop="30dp" />
</RelativeLayout>
留言列表