Android_(控件)使用ImageView簡單實現圖片翻轉


 

 

 效果圖

 

1)可以把圖像的id存放數組中利用setImageResource()setImageDrawable()方法放在數組中便於循環

2)已經是第一張圖像時,再點擊“上一頁”,Toast提示:已經是第一張圖像,並不再往前翻;同樣,已經最后一張圖像時,再點擊“下一頁”,Toast提示:已經最后一張圖像,並不再往

 

 給出源代碼

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.example.asus.a161304049_gary03.MainActivity">


    <LinearLayout
        android:id="@+id/Layoutt"
        android:layout_width="match_parent"
        android:layout_height="215dp"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/imagexianshi"
            android:layout_width="match_parent"
            android:layout_height="180dp"
            app:srcCompat="@drawable/qzu1" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <Button
                android:id="@+id/Back"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="上一張" />

            <Button
                android:id="@+id/Next"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="下一張" />
        </LinearLayout>

    </LinearLayout>

</FrameLayout>
activity_main.xml

 

package com.example.asus.a161304049_gary03;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Button;
import  android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    //用一個數組來保存圖片
       int[] images = new int[]{
            R.drawable.qzu1,
            R.drawable.qzu2,
            R.drawable.qzu3,
            R.drawable.qzu5
    };
    //作為圖片的下標
    int countImg = 0;
    //前進和后退的按鈕
    private Button B1,B2;
    //用ImageView來存放圖片
    private ImageView Xianshi;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        B1 = (Button) findViewById(R.id.Back);
        B2 = (Button) findViewById(R.id.Next);
        Xianshi = (ImageView) findViewById(R.id.imagexianshi);

        Xianshi.setImageResource(images[0]);

        B1.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                //這里就是一個簡單的邏輯啦
                //如果選中的圖片是第一張,那么就不能再選擇前面的一張。輸出Toast提示的消息
                if(countImg>0){
                    countImg--;
                    //用setImageResource來顯示圖片
                    Xianshi.setImageResource(images[countImg]);
                }else{
                    Toast.makeText(MainActivity.this,"已經是第一張圖像,並不再往前翻",Toast.LENGTH_SHORT).show();
                }
            }
        });

        B2.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                //如果選中的圖片是最后,那么就不能再選擇后面的一張。輸出Toast提示的消息
                if(countImg<3){
                    countImg++;
                    Xianshi.setImageResource(images[countImg]);
                }else{
                    Toast.makeText(MainActivity.this,"已經是最后一張圖像,並不再往后翻",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
MainActivity

 

 

ImageView控件用來存放圖片展示出來,xml屬性不是本文重點,這里就不再介紹。

 

 

第一步:定義成員變量

    //用一個數組來保存圖片
       int[] images = new int[]{
            R.drawable.qzu1,
            R.drawable.qzu2,
            R.drawable.qzu3,
            R.drawable.qzu5
    };
    //作為圖片的下標
    int countImg = 0;
    //前進和后退的按鈕
    private Button B1,B2;
    //用ImageView來存放圖片
    private ImageView Xianshi;

 

 

第二步:實現按鈕"上一張"和"下一張"的事件響應機制

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        B1 = (Button) findViewById(R.id.Back);
        B2 = (Button) findViewById(R.id.Next);
        Xianshi = (ImageView) findViewById(R.id.imagexianshi);

        Xianshi.setImageResource(images[0]);

        B1.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                //這里就是一個簡單的邏輯啦
                //如果選中的圖片是第一張,那么就不能再選擇前面的一張。輸出Toast提示的消息
                if(countImg>0){
                    countImg--;
                    //用setImageResource來顯示圖片
                    Xianshi.setImageResource(images[countImg]);
                }else{
                    Toast.makeText(MainActivity.this,"已經是第一張圖像,並不再往前翻",Toast.LENGTH_SHORT).show();
                }
            }
        });

        B2.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                //如果選中的圖片是最后,那么就不能再選擇后面的一張。輸出Toast提示的消息
                if(countImg<3){
                    countImg++;
                    Xianshi.setImageResource(images[countImg]);
                }else{
                    Toast.makeText(MainActivity.this,"已經是最后一張圖像,並不再往后翻",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

 

傳送門:消息模式Toast.make Text的幾種常見用法

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM