사용한 툴 : 이클립스

사용 버전 : API 16

첨부 파일 : AndImageViewer.zip



MainActivity.java

 

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
package com.andimageviewer;
 
import java.io.File;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
 
public class MainActivity extends Activity {
    Button btnPre,btnNext;
    myPictureView myPicture;
    int curNum;
    File[] imageFiles;
    String imageFname;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("간단 이미지 뷰어");
        
        btnPre=(Button)findViewById(R.id.btnPrev);
        btnNext=(Button)findViewById(R.id.btnNext);
        myPicture=(myPictureView)findViewById(R.id.myPictureView1);
        
        imageFiles = new File("/sdcard/pictures").listFiles();
        imageFname = imageFiles[0].toString();
        myPicture.imagePath = imageFname;
        
        btnPre.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(curNum <=0)
                {
                    Toast.makeText(getApplicationContext(), "첫번째 그림입니다.", Toast.LENGTH_SHORT).show();
                }else{
                    curNum--;
                    setMyPicture();
                }
            }
        });
        btnNext.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                if(curNum>=imageFiles.length-1)
                {
                    Toast.makeText(getApplicationContext(), "마지막 그림입니다.", Toast.LENGTH_SHORT).show();
                }
                else{
                    curNum++;
                    setMyPicture();
                }
            }
        });
    }
    private void setMyPicture()
    {
        imageFname=imageFiles[curNum].toString();
        myPicture.imagePath = imageFname;
        myPicture.invalidate();
    }
}
 
 
cs


myPictureView.java

 

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
 
package com.andimageviewer;
 
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
 
public class myPictureView extends View {
    String imagePath = null;
    public myPictureView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }
 
    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        if(imagePath != null)
        {
            Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
            canvas.drawBitmap(bitmap, 00,null);
            bitmap.recycle();
        }
    }
    
}
 
cs


activity_main.xml

 

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
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.andimageviewer.MainActivity" >
 
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button 
            android:id = "@+id/btnPrev"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="이전 그림"/>
        <Button 
            android:id = "@+id/btnNext"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="다음 그림"/>
    </LinearLayout>
    <com.andimageviewer.myPictureView 
        android:id="@+id/myPictureView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>
 
cs


sdcard/Picture파일에 있는 이미지 파일을 가져와 보여주는 Simple Application

가상머신에서 구동시 DDMS로 (안드로이드 스튜디오는 Android Device Monitor를 이용) sdcard/Picture 폴더에 이미지를 넣어 실행하면 된다.


AndImageViewer.zip


+ Recent posts