關於ViewSwitcher(圖片切換)

ViewSwitcher是用於圖片之間的輪換,

下面簡單的寫幾個demo:

點擊上一張的效果
點擊下一張的效果

看看代碼是怎麼寫的吧:

Layout代碼:

<?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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="chauncy.example.com.day04_class1220.MainActivity">

    <RelativeLayout
        android:id="@+id/relative_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/img_head"
        >

        <Button
            android:id="@+id/btn_next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"

            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginEnd="32dp"
            android:layout_marginRight="32dp"
            android:text="下一張"
            />

        <Button
            android:id="@+id/btn_pre"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="37dp"
            android:text="上一張"
            />

    </RelativeLayout>

    <EditText
        android:id="@+id/edit_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/relative_btn"
        android:hint="姓名:"
        />

    <EditText

        android:id="@+id/edit_pwd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/edit_name"
        android:hint="密碼:"
        />

    <Button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/edit_pwd"
        android:text="登錄"
        />

    <ImageView
        android:scaleType="centerCrop"
        android:id="@+id/img_head"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_marginTop="28dp"
        android:src="@mipmap/ic_launcher"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

java代碼:

package chauncy.example.com.day04_class1220;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    //下標
    private int position = 0;
    private ImageView img_head;
    private Button btn_pre, btn_next, btn_login;
    private EditText edit_name, edit_pwd;


    //圖片:  數組/集合   ImageView?  Image?  View?  R.mipmap.xxx
    //圖片的數組
    private int[] imgs = {R.mipmap.ic_launcher, R.mipmap.w01, R.mipmap.w02, R.mipmap.w03, R.mipmap.w04, R.mipmap.w05};
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        img_head = (ImageView) findViewById(R.id.img_head);
        btn_pre = (Button) findViewById(R.id.btn_pre);
        btn_next = (Button) findViewById(R.id.btn_next);
        btn_login = (Button) findViewById(R.id.btn_login);
        edit_name = (EditText) findViewById(R.id.edit_name);
        edit_pwd = (EditText) findViewById(R.id.edit_pwd);

        //設置點擊事件
        btn_pre.setOnClickListener(this);
        btn_next.setOnClickListener(this);
        btn_login.setOnClickListener(this);


        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }
    /**
     * @param v-->指代的點擊的按鈕
     */
    @Override
    public void onClick(View v) {
        //3個按鈕的點擊事件
        //1:判斷是哪個按鈕
        switch (v.getId()) {
            case R.id.btn_pre:

                //1:找到ImageView
                //2:換IamgeView中的圖片
                position--;
                if (position < 0) {
                    position = imgs.length - 1;
                }

                img_head.setImageResource(imgs[position]);
//                Toast.makeText(this, "點擊了上一張", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_next:
                position++;
                if (position > imgs.length - 1) {
                    position = 0;
                }

                img_head.setImageResource(imgs[position]);
//                Toast.makeText(this, "點擊了下一張", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_login:

                //跳轉頁面
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                //啓動Activity
                MainActivity.this.startActivity(intent);
                break;
        }
    }
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    public Action getIndexApiAction() {
        Thing object = new Thing.Builder()
                .setName("Main Page") // TODO: Define a title for the content shown.
                // TODO: Make sure this auto-generated URL is correct.
                .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
                .build();
        return new Action.Builder(Action.TYPE_VIEW)
                .setObject(object)
                .setActionStatus(Action.STATUS_TYPE_COMPLETED)
                .build();
    }

    @Override
    public void onStart() {
        super.onStart();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client.connect();
        AppIndex.AppIndexApi.start(client, getIndexApiAction());
    }

    @Override
    public void onStop() {
        super.onStop();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        AppIndex.AppIndexApi.end(client, getIndexApiAction());
        client.disconnect();
    }
}

**

希望這篇博客對你有幫助!謝謝每一個可愛的你!

**

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章