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);
            }
        }
    }


}

以下是实现的结果图:

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值