Android實現對圖片的瀏覽功能
主要是實現將圖片的瀏覽功能,以下程式碼是實現介面:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.home1.MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="400dp"
android:id="@+id/img_iv"
android:src="@drawable/first"
android:background="#2727"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/img_iv"
android:id="@+id/layout_ll"
android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/img_iv"
android:id="@+id/before_bt"
android:text="前一張"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/img_iv"
android:layout_toRightOf="@id/before_bt"
android:id="@+id/first_bt"
android:text="第一張"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/img_iv"
android:layout_toRightOf="@id/first_bt"
android:id="@+id/end_bt"
android:text="最後一張"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/img_iv"
android:layout_toRightOf="@id/end_bt"
android:id="@+id/after_bt"
android:text="後一張"/>
</LinearLayout>
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/change_sb"
android:max="9"
android:layout_below="@id/layout_ll"/>
</RelativeLayout>
以下程式碼是對程式碼的實現:
package com.example.administrator.home1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private static final String TEST = "test";
// 定義按鈕物件
private ImageView mImageIv;
private Button mBeforeBt;
private Button mFirstBt;
private Button mEndBt;
private Button mAfterBt;
private SeekBar mChangeSb;
private int i = 0;
private int []img = new int[]{R.drawable.one,R.drawable.two,R.drawable.three,R.drawable.four,R.drawable.five,R.drawable.six,R.drawable.seven,R.drawable.eight,R.drawable.nine,R.drawable.ten};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
private void initViews() {
// 1、獲取代表控制元件的物件
mImageIv = (ImageView)findViewById(R.id.img_iv);
mBeforeBt = (Button)findViewById(R.id.before_bt);
mFirstBt = (Button)findViewById(R.id.first_bt);
mEndBt = (Button)findViewById(R.id.end_bt);
mAfterBt = (Button)findViewById(R.id.after_bt);
mChangeSb = (SeekBar)findViewById(R.id.change_sb);
// 3、生成監聽器物件
MyOnClickListener listener = new MyOnClickListener();
// 4、為控制元件繫結監聽器物件
mFirstBt.setOnClickListener(listener);
mBeforeBt.setOnClickListener(listener);
mAfterBt.setOnClickListener(listener);
mEndBt.setOnClickListener(listener);
}
// 2、定義一個類,實現監聽器介面
class MyOnClickListener implements View.OnClickListener{
@Override
public void onClick(View v) {
// 判斷被點選的按鈕id
switch (v.getId()){
case R.id.first_bt:
i=0;
mImageIv.setImageResource(img[i]);
mChangeSb.setProgress(i);
break;
case R.id.before_bt:
if(i == 0){
Log.i(TEST,"已經是第一張了");
Toast.makeText(MainActivity.this,"這已經是第一張了",Toast.LENGTH_SHORT).show();
}else{
mImageIv.setImageResource(img[--i]);
mChangeSb.setProgress(i);
}
break;
case R.id.after_bt:
if(i == 9){
Log.i(TEST,"已經是最後一張了");
Toast.makeText(MainActivity.this,"這已經是最後一張了",Toast.LENGTH_SHORT).show();
}else{
mImageIv.setImageResource(img[++i]);
mChangeSb.setProgress(i);
}
break;
case R.id.end_bt:
i=9;
mImageIv.setImageResource(img[i]);
mChangeSb.setProgress(i);
}
}
}
}
以下是實現的結果圖: