1. 멀티터치(Multitouch)


package org.android.soldesk.multitouch_01;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.FloatMath;
import android.view.MotionEvent;

public class Multitouch_01Activity extends AppCompatActivity {


MultitouchView mMultitouchView;

private final static int NORMAL_MODE = 1;
private final static int ZOOM_MODE = 2;

private float oldDistance;
private float newDistance;
private int mode = NORMAL_MODE;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_multitouch_01);

mMultitouchView = new MultitouchView(this);

setContentView(mMultitouchView);
}

public boolean onTouchEvent(MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_POINTER_2_DOWN:
{
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);

oldDistance = (float) Math.sqrt(x*x + y*y);
newDistance = oldDistance;

if (oldDistance > 10f)
{
mode = ZOOM_MODE;
}
else
{
mode = NORMAL_MODE;
oldDistance = 0.0f;
newDistance = 0.0f;
}
}
break;

case MotionEvent.ACTION_MOVE:
{
if (mode == ZOOM_MODE)
{
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);

newDistance = (float) Math.sqrt(x*x + y*y);

if (newDistance > 10f)
{
float scale = newDistance / oldDistance;
mMultitouchView.setScale(scale);
mMultitouchView.invalidate();
}
else {
mode = NORMAL_MODE;
oldDistance = 0.0f;
newDistance = 0.0f;
}
}
}
break;
}
return super.onTouchEvent(event);
}
}

<java>


package org.android.soldesk.multitouch_01;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.View;

public class MultitouchView extends View{

private int posX = 0;
private int posY = 0;
private float scale = 1f;

private Bitmap image;

public MultitouchView(Context context) {
super(context);

image = BitmapFactory.decodeResource(context.getResources(),R.drawable.ab);
}

public void setScale(float scale)
{
this.scale = scale;
}

@Override
protected void onDraw(Canvas canvas)
{
if (image != null)
{
Rect srcRect = new Rect(0,0,image.getWidth(),image.getHeight());

int width = (int) (image.getWidth()*scale);
int height = (int) (image.getHeight()*scale);

posX = (image.getWidth() - width) /2;
posY = (image.getHeight() - height) /2;

Rect dstRect;

dstRect = new Rect(posX,posY,posX+width,posY+height);

canvas.drawBitmap(image,srcRect,dstRect,null);

}
}
}

<MultitouchView>




2. 인텐트(Intent)


<?xml version="1.0" encoding="utf-8"?>
<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="org.android.soldesk.intentdto.IntentDtoActivity">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editName"
android:ems="10"
android:hint="이름을 입력하세요"
>
<requestFocus/>
</EditText>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editAge"
android:ems="10"
android:hint="나이를 입력하세요"
/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editAddress"
android:ems="10"
android:hint="주소를 입력하세요"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editAa"
android:ems="10"
android:hint="월급을 입력하세요"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnSend"
android:text="전송"
/>
</LinearLayout>

<xml>


package org.android.soldesk.intentdto;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class IntentDtoActivity extends AppCompatActivity {

EditText editName;
EditText editAge;
EditText editAddress;
EditText editAa;
Button btnSend;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intent_dto);

editName = (EditText) findViewById(R.id.editName);
editAge = (EditText) findViewById(R.id.editAge);
editAddress = (EditText) findViewById(R.id.editAddress);
editAa = (EditText) findViewById(R.id.editAa);

btnSend = (Button) findViewById(R.id.btnSend);

btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

// 입력 데이타 얻기
String name = editName.getText().toString();
String age = editAge.getText().toString();
String address = editAddress.getText().toString();
String aa = editAa.getText().toString();

// 직렬화 객체에 저장

Person pp = new Person();
pp.setName(name);
pp.setAge(Integer.parseInt(age));
pp.setAddress(address);
pp.setAa(Integer.parseInt(aa));

// 인텐트 생성 및 직렬화 객체 저장

Intent intent = new Intent(getApplicationContext(),NextActivity.class);
intent.putExtra("person", pp);

startActivity(intent);
}
});
}
}

<java>


package org.android.soldesk.intentdto;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class NextActivity extends AppCompatActivity {

TextView txtResult;
Button btnBack;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.next);

txtResult = (TextView) findViewById(R.id.txtResult);

// 인텐트 얻기

Intent intent = getIntent();

// 직렬화 객체 얻기
Person pp = (Person) intent.getSerializableExtra("person");

String name = pp.getName();
int age = pp.getAge();
String address =pp.getAddress();
int aa = pp.getAa();

String msg = "이름 : " +name+ ", 나이 : " +age+ ", 주소 : "+address+", 월급 : "+(aa*1.5) ;
txtResult.setText(msg);

btnBack = (Button) findViewById(R.id.btnBack);

btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}

<NextActivity.java>


package org.android.soldesk.intentdto;


import java.io.Serializable;

public class Person implements Serializable{

String name;
int age;
String address;
int aa;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public int getAa() {
return aa;
}

public void setAa(int aa) {
this.aa = aa;
}
}

<Person.java>


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.android.soldesk.intentdto">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".IntentDtoActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<!-- 액티비티 등록 -->
<activity
android:name=".NextActivity"
android:label="NextActivity 이에요 ~~~~~"
/>
</application>

</manifest>

<AndroidManifest.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="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtResult"
android:text="전송된 데이타 출력"
android:textAppearance="?android:attr/textAppearanceLarge"
/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnBack"
android:text="뒤로가자~~~"
/>

</LinearLayout>

<next.xml>




3. Intent_Bundle


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="org.android.soldesk.intent_bundle_01.Intent_Bundle_01Activity">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editName"
android:ems="10"
android:hint="이름을 입력하세요"
>

<requestFocus/>
</EditText>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editAge"
android:ems="10"
android:hint="나이를 입력하세요"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editAddress"
android:ems="10"
android:hint="주소를 입력하세요"
/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editAa"
android:ems="10"
android:hint="전화번호를 입력하세요"
/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnSend"
android:text="전송송~~~"
/>

</LinearLayout>

<xml>


package org.android.soldesk.intent_bundle_01;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Intent_Bundle_01Activity extends AppCompatActivity {

EditText editName;
EditText editAge;
EditText editAddress;
EditText editAa;
Button btnSend;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intent__bundle_01);

editName = (EditText) findViewById(R.id.editName);
editAge = (EditText) findViewById(R.id.editAge);
editAddress = (EditText) findViewById(R.id.editAddress);
editAa = (EditText) findViewById(R.id.editAa);

btnSend = (Button) findViewById(R.id.btnSend);

btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

// 입력 데이타 얻기

String name = editName.getText().toString();
String age = editAge.getText().toString();
String address = editAddress.getText().toString();
String aa = editAa.getText().toString();

// Bundle 객체에 저장
Bundle bundle = new Bundle();

// Bundle 는 key, value 쌍으로 이루어진
// 일종의 해쉬 맵 자료 구조

bundle.putString("name",name);
bundle.putInt("age",Integer.parseInt(age));
bundle.putString("address",address);
bundle.putString("aa",aa);

//인텐트 생성 및 데이타 저장

Intent intent = new Intent(getApplicationContext(),NextActivity.class);
intent.putExtra("bundle",bundle);
startActivity(intent);
}
});
}
}

<java>


package org.android.soldesk.intent_bundle_01;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class NextActivity extends AppCompatActivity {

TextView txtResult;
Button btnBack;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.next);

txtResult = (TextView) findViewById(R.id.txtResult);

// 인텐트 얻기
Intent intent = getIntent();

// Bundle 객체 얻기
Bundle bundle = intent.getBundleExtra("bundle");

String name = bundle.getString("name");
int age = bundle.getInt("age");
String address = bundle.getString("address");
String aa = "010-";
aa += bundle.getString("aa");

String msg = " \n 이름 : " +name+ " \n 나이 : " +age+ " \n 주소 : " +address+ " \n 전화번호 : "+aa;

txtResult.setText(msg);

btnBack = (Button) findViewById(R.id.btnBack);

btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}

<NextActivity>


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.android.soldesk.intent_bundle_01">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Intent_Bundle_01Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".NextActivity"
android:label="NextActivity Test ~~~~~"/>
</application>

</manifest>

<AndroidManifest.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="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtResult"
android:text="전달된 Bundle 데이타 출력"
android:textAppearance="?android:attr/textAppearanceLarge"
/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnBack"
android:text="뒤뒤로~~~~"
/>
</LinearLayout>

<next.xml>




4. Result_Return


<?xml version="1.0" encoding="utf-8"?>
<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="org.android.soldesk.result_return_01.Result_Return_01Activity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editValue01"
android:ems="10"
android:hint="첫번째 값을 입력하세요"
>
<requestFocus/>
</EditText>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editValue02"
android:ems="10"
android:hint="두번째 값을 입력하세요"
/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editValue03"
android:ems="10"
android:hint="세번째 값을 입력하세요"
/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/butAdd"
android:text="+"
/>

<Button
android:id="@+id/butSub"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="-" />

<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/butMul"
android:text="*"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/butDiv"
android:text="/"
/>

</LinearLayout>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtResult"
android:text="결과는 여기에 나온다"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
</LinearLayout>
</LinearLayout>

<xml>


package org.android.soldesk.result_return_01;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Result_Return_01Activity extends AppCompatActivity implements View.OnClickListener{

EditText editValue1;
EditText editValue2;
EditText editValue3;

Button btnAdd;
Button btnSub;
Button btnMul;
Button btnDiv;

TextView txtResult;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result__return_01);

editValue1 = (EditText) findViewById(R.id.editValue01);
editValue2 = (EditText) findViewById(R.id.editValue02);
editValue3 = (EditText) findViewById(R.id.editValue03);

btnAdd = (Button) findViewById(R.id.butAdd);
btnSub = (Button) findViewById(R.id.butSub);
btnMul = (Button) findViewById(R.id.butMul);
btnDiv = (Button) findViewById(R.id.butDiv);

txtResult = (TextView) findViewById(R.id.txtResult);

btnAdd.setOnClickListener(this);
btnSub.setOnClickListener(this);
btnMul.setOnClickListener(this);
btnDiv.setOnClickListener(this);

}

@Override
public void onClick(View v) {

// 입력값과 연산자 얻기

double value1 = Double.parseDouble(editValue1.getText().toString());
double value2 = Double.parseDouble(editValue2.getText().toString());
double value3 = Double.parseDouble(editValue3.getText().toString());

char op = ((Button)v).getText().charAt(0);

// 인텐트 생성 및 전달

Intent intent = new Intent(getApplicationContext(),CalcActivity.class);
intent.putExtra("value1",value1);
intent.putExtra("value2",value2);
intent.putExtra("value3",value3);
intent.putExtra("op",op);

startActivityForResult(intent, 1); // requestCode : 1 는 요청을 식별
// 객체를 전달하고 전달한 Activity로 부터 결과값을 onActivityResult 가 받음
}
@Override
protected void onActivityResult(int requestCode,int resultCode, Intent data) {
super.onActivityResult(requestCode,resultCode,data);

switch (resultCode)
{
case AppCompatActivity.RESULT_OK:
Double result = data.getDoubleExtra("result", 0.0);
txtResult.setText("결과 : "+Double.toString(result));
break;
}
}
}

<java>


package org.android.soldesk.result_return_01;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class CalcActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// 인텐트 값 전달

Intent intent = getIntent();

double value01 = intent.getDoubleExtra("value1",0);
double value02 = intent.getDoubleExtra("value2",0);
double value03 = intent.getDoubleExtra("value3",0);

char op = intent.getCharExtra("op",'+');

double result = 0;

switch (op)
{
case '+':
result = (value01 - value02) + value03;
break;
case '-':
result = (value01 + value02) - value03;
break;
case '*':
result = (value01 + value02) * value03;
break;
case '/':
result = (value01 - value02) / value03;
break;
}

// 결과 값 전달
intent.putExtra("result",result);
setResult(Activity.RESULT_OK,intent);

finish();

}
}

<CalcActivity.java>


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.android.soldesk.result_return_01">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Result_Return_01Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".CalcActivity"
android:label="CalcActivity Test"/>
</application>

</manifest>

<AndroidManifest.xml>


+ Recent posts