Android Studio java 實現簡單的圖片查看器{學習記錄}


1,需求樣式

 

2,建立viewer.xml文件,選擇LinearLayout布局

使用ImageView控件和Buttons控件

如圖:

 

代碼如下:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="vertical">
 7 
 8     <ImageView
 9         android:id="@+id/img"
10         android:layout_width="match_parent"
11         android:layout_height="522dp"
12         android:scaleType="centerInside"
13         app:srcCompat="@mipmap/a" />
14 
15     <LinearLayout
16         android:layout_width="match_parent"
17         android:layout_height="55dp"
18         android:orientation="horizontal">
19 
20         <Button
21             android:id="@+id/last"
22             android:layout_width="0dp"
23             android:layout_height="wrap_content"
24             android:layout_weight="1"
25             android:text="上一張" />
26 
27         <Button
28             android:id="@+id/next"
29             android:layout_width="0dp"
30             android:layout_height="wrap_content"
31             android:layout_weight="1"
32             android:text="下一張" />
33     </LinearLayout>
34 </LinearLayout>

注:在使用button控件時我們需要兩個,分別是“上一張”和“下一張”,這里把兩個組件放到一個橫向布局里(第15行)

這里需要在button控件里使用weight,這個管理的是橫向布局里的兩個button的寬度占比,對應到垂直布局則控制高度占比

android:layout_weight="1"

我們需要把兩個button的寬度改為0dp,然后設置weight值都為1,意思為各占一半

如果一個設置為1,另一個設置為3,則第一個寬度占比為1,第二個寬度占比為3.

對應到垂直布局道理類似

3,新建ViewerActivity.java

代碼如下:

 1 package com.example.myapplication;
 2 import android.os.Bundle;
 3 import android.view.View;
 4 import android.widget.Button;
 5 import android.widget.ImageView;
 6 
 7 import androidx.appcompat.app.AppCompatActivity;
 8 
 9 public class ViewerActivity extends AppCompatActivity implements View.OnClickListener{
10     private Button last;
11     private Button next;
12     private ImageView img;
13     private int[] p = {R.mipmap.a,R.mipmap.b,R.mipmap.c};//圖片組 R.圖片目錄.圖片名
14     private int Index = 0;  //圖片的當前索引位置
15     private int count = p.length-1;
16     @Override
17     protected void onCreate(Bundle savedInstanceState) {
18         super.onCreate(savedInstanceState);//創造
19         setContentView(R.layout.viewer);///設置內容視圖
20         init();//初始化
21     }
22     //初始化
23     private void init() {
24         last = (Button) findViewById(R.id.last);//按ID查找控件
25         last.setOnClickListener(this);//單擊設置監聽器
26         next = (Button)findViewById(R.id.next);
27         next.setOnClickListener(this);
28         img =(ImageView) findViewById(R.id.img);
29     }
30     //單擊事件
31     public void onClick(View view) {
32         //view.getID獲取ID,判斷是哪一個
33         switch (view.getId()) {
34             case R.id.last:
35                 //如果當前圖片是第一張,則上一張圖片為最后一張圖片
36                 if (Index == 0) {
37                     Index = count;
38                 } else {
39                     //否則改為上一張圖片
40                     Index = Index - 1;
41                 }
42                 break;
43             case R.id.next:
44                 //如果當前圖片是最后一張,則下一張圖片為第一張圖片
45                 if (Index == count) {
46                     Index = 0;
47                 } else {
48                     //否則改為下一張圖片
49                     Index = Index + 1;
50                 }
51                 break;
52             default:
53                 break;
54         }
55         img.setImageResource(p[Index]);   //顯示圖片
56     }
57 }

其中第14行定義了一個圖片組,mipmap為圖片所在目錄,其后的a,b,c為圖片的名稱

private int[] p = {R.mipmap.a,R.mipmap.b,R.mipmap.c};//圖片組 R.圖片目錄.圖片名

放圖片的目錄mipmap可以換,圖片名是根據自己的圖片名設置的

如果你的圖片名為d.png,則相應代碼要改為R.mipmap.d

規則:R.圖片目錄.圖片名

其中圖片的名字不可包含空格

4,效果圖如下:

 

 

 

 
       


免責聲明!

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



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