Home 두개의 이미지 뷰에 이미지 번갈아 보여주기
Post
Cancel

두개의 이미지 뷰에 이미지 번갈아 보여주기

두개의 이미지 뷰에 이미지 번갈아 보여주기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  android:layout_width="match_parent"  
  android:layout_height="match_parent"  
  >  
  
 <HorizontalScrollView  
  android:id="@+id/scrollView01"  
  android:layout_width="match_parent"  
  android:layout_height="match_parent"  
  android:layout_above="@+id/buttonLayout"  
  android:layout_alignParentTop="true"  
  >  
 <ScrollView  
  android:layout_width="match_parent"  
  android:layout_height="match_parent">  
  
 <ImageView  
  android:id="@+id/imageView01"  
  android:layout_width="wrap_content"  
  android:layout_height="wrap_content"  
  android:scaleType="matrix"  
  />  
  
 </ScrollView>  
 </HorizontalScrollView>  
  
 <LinearLayout  
  android:id="@+id/buttonLayout"  
  android:orientation="vertical"  
  android:layout_width="match_parent"  
  android:layout_height="wrap_content"  
  android:layout_centerInParent="true"  
  >  
 <LinearLayout  
  android:orientation="horizontal"  
  android:layout_width="wrap_content"  
  android:layout_height="wrap_content"  
  android:layout_gravity="center_horizontal"  
  >  
 <Button  
  android:id="@+id/button01"  
  android:layout_width="wrap_content"  
  android:layout_height="wrap_content"  
  android:layout_margin="10dp"  
  android:text="    ▲    "  
  android:textSize="18sp"  
  />  
 <Button  
  android:id="@+id/button02"  
  android:layout_width="wrap_content"  
  android:layout_height="wrap_content"  
  android:layout_margin="10dp"  
  android:text="    ▼    "  
  android:textSize="18sp"  
  />  
 </LinearLayout>  
 </LinearLayout>  
 <HorizontalScrollView  
  android:id="@+id/scrollView02"  
  android:layout_width="match_parent"  
  android:layout_height="match_parent"  
  android:layout_below="@+id/buttonLayout"  
  android:layout_alignParentBottom="true"  
  >  
 <ScrollView  
  android:layout_width="match_parent"  
  android:layout_height="match_parent">  
  
 <ImageView  
  android:id="@+id/imageView02"  
  android:layout_width="wrap_content"  
  android:layout_height="wrap_content"  
  android:scaleType="matrix"  
  />  
  
 </ScrollView>  
 </HorizontalScrollView>  
  
</RelativeLayout>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package org.techtown.myapplication;  
  
import android.os.Bundle;  
import android.view.View;  
import android.widget.Button;  
import android.widget.ImageView;  
  
import androidx.appcompat.app.AppCompatActivity;  
  
import org.techtown.myapplication.R;  
  
public class MainActivity extends AppCompatActivity {  
  ImageView imageView01;  
  ImageView imageView02;  
  
  @Override  
  protected void onCreate(Bundle savedInstanceState) {  
  super.onCreate(savedInstanceState);  
  setContentView(R.layout.activity_main);  
  
  imageView01 = findViewById(R.id.imageView01);  
  imageView02 = findViewById(R.id.imageView02);  
  
  Button button01 = findViewById(R.id.button01);  
  button01.setOnClickListener(new View.OnClickListener() {  
  public void onClick(View v) {  
  moveImageUp();  
 } });  
  Button button02 = findViewById(R.id.button02);  
  button02.setOnClickListener(new View.OnClickListener() {  
  public void onClick(View v) {  
  moveImageDown();  
 } });  
  moveImageUp();  
 }  
  private void moveImageDown() {  
  imageView01.setImageResource(0);  
  imageView02.setImageResource(R.drawable.one);  
  
  imageView01.invalidate();  
  imageView02.invalidate();  
 }  
  private void moveImageUp() {  
  imageView01.setImageResource(R.drawable.one);  
  imageView02.setImageResource(0);  
  
  imageView01.invalidate();  
  imageView02.invalidate();  
 }  
}

invalidate()는 draw를 다시 하도록 요청하고 requestLayout()은 measure를 통한 사이즈 체크부터 다시 시작한다.

ezgif com-gif-maker

This post is licensed under CC BY 4.0 by the author.