zoukankan      html  css  js  c++  java
  • Android ImageView功能的实现

    例题2-13

    activity_main.xml

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <!--suppress ALL -->
     3 <LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
     4     android:layout_width="fill_parent"
     5     android:layout_height="fill_parent"
     6     android:orientation="vertical"
     7     android:gravity="center"
     8     xmlns:android="http://schemas.android.com/apk/res/android">
     9     <LinearLayout
    10         android:layout_width="wrap_content"
    11         android:layout_height="wrap_content">
    12 
    13         <ImageView android:id="@+id/image"
    14             android:layout_width="355dip"
    15             android:layout_height="650dip"
    16             android:src="@drawable/p2"/>
    17     </LinearLayout>
    18     <LinearLayout
    19         android:layout_width="match_parent"
    20         android:layout_height="fill_parent">
    21         <Button android:id="@+id/up"
    22             android:layout_width="150dip"
    23             android:layout_height="wrap_content"
    24             android:text="上一张"/>
    25         <Button android:id="@+id/down"
    26             android:layout_width="150dip"
    27             android:layout_height="wrap_content"
    28             android:text="下一张"/>
    29     </LinearLayout>
    30 </LinearLayout>

    Mainactivity.java代码

     1 package com.example.hello;
     2 
     3 import androidx.appcompat.app.AppCompatActivity;
     4 
     5 import android.os.Bundle;
     6 import android.telephony.SmsManager;
     7 import android.view.View;
     8 import android.widget.Button;
     9 import android.widget.CheckBox;
    10 import android.widget.ImageView;
    11 import android.widget.ProgressBar;
    12 import android.widget.RadioButton;
    13 import android.widget.TextView;
    14 
    15 public class MainActivity extends AppCompatActivity {
    16     Button Button1,Button2;
    17     int[] images={
    18             R.drawable.p2, R.drawable.p3, R.drawable.p4, R.drawable.p5,
    19             R.drawable.p6, R.drawable.p7, R.drawable.p8, R.drawable.p9,
    20             R.drawable.p10, R.drawable.p11, R.drawable.p12, R.drawable.p1
    21     };
    22     ImageView image;
    23     int index=0;
    24     @Override
    25     protected void onCreate(Bundle savedInstanceState) {
    26         super.onCreate(savedInstanceState);
    27         setContentView(R.layout.activity_main);
    28         Button1 = (Button)findViewById(R.id.up);
    29         Button2 = (Button)findViewById(R.id.down);
    30         image = (ImageView)findViewById(R.id.image);
    31         Button1.setOnClickListener(new mclik());
    32         Button2.setOnClickListener(new mclik());
    33     }
    34 
    35     class mclik implements View.OnClickListener{
    36 
    37         @Override
    38         public void onClick(View v) {
    39             if(v==Button1){
    40                 if(index>0&&index<images.length){
    41                     index--;
    42                     image.setImageResource(images[index]);
    43                 }else {
    44                     index=images.length-1;
    45                     image.setImageResource(images[index]);
    46                 }
    47             }
    48             if(v==Button2){
    49                 if(index>=0&&index<images.length-1){
    50                     index++;
    51                     image.setImageResource(images[index]);
    52                 }else {
    53                     index=0;
    54                     image.setImageResource(images[index]);
    55                 }
    56             }
    57         }
    58     }
    59 
    60 }
  • 相关阅读:
    Android总结篇系列:Activity生命周期
    设计模式总结篇系列:策略模式(Strategy)
    设计模式总结篇系列:享元模式(Flyweight)
    设计模式总结篇系列:组合模式(Composite)
    设计模式总结篇系列:桥接模式(Bridge)
    设计模式总结篇系列:外观模式(Facade)
    设计模式总结篇系列:代理模式(Proxy)
    设计模式总结篇系列:装饰器模式(Decorator)
    设计模式总结篇系列:适配器模式(Adapter)
    设计模式总结篇系列:原型模式(Prototype)
  • 原文地址:https://www.cnblogs.com/xiaowangdatie/p/13726537.html
Copyright © 2011-2022 走看看