2012.11.21 17:45

 학습 목표: 사용자 정보 입력 앱 만들기

* 학습 과제: 사용자 토스트 정의와 사용자 정보 입력 앱을 만든다.

* 프로젝트 정보

 - 빌드 SDK : Google API 15

 - 액티비티 이름 : UserInfo

<실습 내용>

- 레이아웃(메인 레이아웃과 다이얼로그 창 레이아웃, 토스트 레이아웃을 구현한다)

dialog1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dip"
        android:text="사용자 이름"/>

    <EditText
        android:id="@+id/dlgEdt1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dip" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dip"
        android:text="이메일"/>

    <EditText
        android:id="@+id/dlgEdt2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dip" />

</LinearLayout>

main.xml

<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:gravity="center_horizontal"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dip"
        android:text="사용자 이름" />

    <TextView
        android:id="@+id/tvEmail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dip"
        android:text="이메일" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dip"
        android:background="#ffff00"
        android:text="여기를  클릭하세요^^" />

</LinearLayout>

toast1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="center"
    android:background="#ffff00">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_action_search"/>
    <TextView
        android:id="@+id/toastText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dip"
        android:text="TextView"/>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_action_search"/>
</LinearLayout>

- java 코딩

UserInfo_Activity.java

package com.example.userinfo;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class UserInfo_Activity extends Activity {
 TextView tvName, tvEmail;
 Button button1;
 EditText dlgEdtName, dlgEdtEmail;
 TextView toastText;
 View dialogView, toastView;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  setTitle("사용자 정보 입력");

  tvName = (TextView) findViewById(R.id.tvName);
  tvEmail = (TextView) findViewById(R.id.tvEmail);
  button1 = (Button) findViewById(R.id.button1);

  button1.setOnClickListener(new View.OnClickListener() {
   public void onClick(View v) {
    dialogView = (View) View.inflate(UserInfo_Activity.this,
      R.layout.dialog1, null);
    AlertDialog.Builder dlg = new AlertDialog.Builder(
      UserInfo_Activity.this);
    dlg.setTitle("사용자 정보 입력");
    dlg.setIcon(R.drawable.ic_action_search);
    dlg.setView(dialogView);
    dlg.setPositiveButton("확인",
      new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog,
         int which) {
        // TODO Auto-generated method stub
        dlgEdtName = (EditText) dialogView
          .findViewById(R.id.dlgEdt1);
        dlgEdtEmail = (EditText) dialogView
          .findViewById(R.id.dlgEdt2);

        tvName.setText(dlgEdtName.getText().toString());
        tvEmail.setText(dlgEdtEmail.getText()
          .toString());
       }
      });

    dlg.setNegativeButton("취소",
      new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog,
         int which) {
        // TODO Auto-generated method stub
        Toast toast = new Toast(UserInfo_Activity.this);
        toastView = (View) View.inflate(
          UserInfo_Activity.this,
          R.layout.toast1, null);
        toastText = (TextView) toastView
          .findViewById(R.id.toastText1);
        toastText.setText("취소했습니다");
        toast.setView(toastView);
        toast.show();
       }
      });
    dlg.show();
   }
  });
 }
}

<AVD 출력 화면>

  

 

 

 

신고
2012.11.14 19:03

* 학습 목표: 이미지 뷰어 만들기

* 학습 과제: SD카드의 특정 폴더의 이미지 파일을 보여주는 간단한 이미지 뷰어를 만든다.

* 프로젝트 정보

 - 빌드 SDK : Google API 15

 - 액티비티 이름 : myPictureView

<실습 내용>

- 레이아웃

main_Activity.xml 

<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:gravity="center_horizontal"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btnPrev"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dip"
            android:layout_weight="1"
            android:background="#ff00ff"
            android:text="이전 그림" />

        <Button
            android:id="@+id/btnNext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dip"
            android:layout_weight="1"
            android:background="#ff00ff"
            android:text="다음 그림" />
    </LinearLayout>

    <com.example.imageviewer.myPictureView
        android:id="@+id/myPictureView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

- java 코딩

ViewerActivity.java

 package com.example.imageviewer;

import java.io.File;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class ViewerActivity extends Activity {
 Button btnPrev, btnNext;
 myPictureView myPicture;
 int curNum;
 File[] imageFiles;
 String imageFname;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        setTitle("간단한 이미지 뷰어");
        
        btnPrev = (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;
        
        btnPrev.setOnClickListener(new View.OnClickListener() {
   
   public void onClick(View v) {
    if (curNum <=0){
     Toast.makeText(getApplicationContext(), "첫번째 그림입니다", 0).show();
    }
    else{
     curNum--;
     imageFname = imageFiles[curNum].toString();
     myPicture.imagePath = imageFname;
     myPicture.invalidate();
    }
   }
  });
       
        btnNext.setOnClickListener(new View.OnClickListener() {
   
   public void onClick(View v) {
    if (curNum >= imageFiles.length-1){
     Toast.makeText(getApplicationContext(), "마지막 그림입니다", 0).show();
    }
    else{
     curNum++;
     imageFname = imageFiles[curNum].toString();
     myPicture.imagePath = imageFname;
     myPicture.invalidate();
    }
   }
  });
        }
}

- 커스텀 위젯 java (onDraw 메소드를 오버라이드 한다)

myPictureView.java

 package com.example.imageviewer;

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, 0, 0, null);
   bitmap.recycle();
  }
 }
}

<AVD 출력 화면>

 

 

 

 

* SDcard를 사용하는 문제로 인해 장시간 작업이 지연되었고, 이를 해결하기 위해 새로운 계정을 통해 이클립스와 AVD 에뮬레이터를 새로 만들어서 해결하였다. 앞으로 안드로이드 앱 개발과정에서만큼은 새로운 계정을 지속적으로 사용할 예정이다.

 

신고
2012.11.08 21:33

-레이아웃

<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" >

    <LinearLayout 

        android:layout_width="match_parent"

        android:layout_height="0dip"

        android:layout_weight="1"

        android:gravity="center"

        android:orientation="horizontal" >

        <ImageButton

            android:id="@+id/ibZoomin" 

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:src="@drawable/zoomin" />

        <ImageButton

            android:id="@+id/ibZoomout" 

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:src="@drawable/zoomout" />

        <ImageButton

            android:id="@+id/ibRotate" 

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:src="@drawable/rotation" />

        <ImageButton

            android:id="@+id/ibBright" 

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:src="@drawable/brighter" />

        <ImageButton

            android:id="@+id/ibDark" 

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:src="@drawable/darken" />

        <ImageButton

            android:id="@+id/ibGray" 

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:src="@drawable/gray" />

    </LinearLayout>

    <LinearLayout 

       android:id="@+id/pic_layout"

       android:layout_width="match_parent"

       android:layout_height="0dip"

        android:layout_weight="9" >

    </LinearLayout>

</LinearLayout>


-코드

package com.example.photoshop;


import android.os.Bundle;

import android.app.Activity;

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.Canvas;

import android.graphics.ColorMatrix;

import android.graphics.ColorMatrixColorFilter;

import android.graphics.Paint;

import android.view.Menu;

import android.view.View;

import android.widget.ImageButton;

import android.widget.LinearLayout;


public class MainActivity extends Activity {

ImageButton zoomIn, zoomOut, rotate, bright, dark, gray;

MyGraphicView gView;

static float scaleX=1, scaleY=1, angle=0, color=1, satur=1;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        setTitle("미니포토샵");

        

        LinearLayout pLayout = (LinearLayout)findViewById(R.id.pic_layout);

        gView = (MyGraphicView) new MyGraphicView(this);

        pLayout.addView(gView);

        

        clickIcons();

        

    }

    

    private void clickIcons() {

    zoomIn = (ImageButton)findViewById(R.id.ibZoomin);

    zoomOut = (ImageButton)findViewById(R.id.ibZoomout);

    rotate = (ImageButton)findViewById(R.id.ibRotate);

    bright = (ImageButton)findViewById(R.id.ibBright);

    dark = (ImageButton)findViewById(R.id.ibDark);

    gray = (ImageButton)findViewById(R.id.ibGray);

   

    zoomIn.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

scaleX += 0.2f;

scaleY += 0.2f;

gView.invalidate();

}

});

    zoomOut.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

scaleX -= 0.2f;

scaleY -= 0.2f;

gView.invalidate();

}

});

    rotate.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

angle += 20;

gView.invalidate();

}

});

    bright.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

color += 0.2f;

gView.invalidate();

}

});

    dark.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

color -= 0.2f;

gView.invalidate();

}

});

    gray.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

if(satur==0) satur=1;

else satur=0;

gView.invalidate();

}

});

    }

    

    private static class MyGraphicView extends View {

    public MyGraphicView(Context context) {

    super(context);

    }

   

    protected void onDraw(Canvas canvas) {

    super.onDraw(canvas);

   

    Bitmap pic = BitmapFactory.decodeResource(getResources(), R.drawable.icon);

    int picX = (this.getWidth()-pic.getWidth())/2;

    int picY = (this.getHeight()- pic.getHeight())/2;

       

    int cenX = this.getWidth()/2;

    int cenY = this.getHeight()/2;

   

    Paint paint = new Paint();

    float[] array = {color, 0, 0, 0, 0,

    0, color, 0, 0, 0,

    0, 0, color, 0, 0,

    0, 0, 0, 1, 0};

    ColorMatrix cm = new ColorMatrix(array);

    if(satur==0) cm.setSaturation(satur);

   

    paint.setColorFilter(new ColorMatrixColorFilter(cm));

   

    canvas.scale(scaleX, scaleY, cenX, cenY);

    canvas.rotate(angle, cenX, cenY);

    canvas.drawBitmap(pic, picX, picY, paint);

   

    pic.recycle();

   

    }

    }

}

-실행화면





저작자 표시 비영리 변경 금지
신고