336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

Android onTouch 에서 ACTION_DOWN 만 들어올경우


ImageView 위에서 터치를 입력 받을 경우


ImageView 에



ImageView imageView = (ImageView) findViewById(R.id.imageView);

imageView.setClickable(true);


Clickable 을 True 로 해주어야 MOVE 나 DOWN 이벤트가 캐치된다.

'Programming > Android' 카테고리의 다른 글

android-screenshot-library  (0) 2013.11.06
Android 졸업 작품 도움 요청 카페  (0) 2013.10.22
Android 실습 0823  (0) 2013.08.23
PinchToZoom ImageView 로 구현  (0) 2013.08.23
Android 채팅 소스 05  (1) 2013.08.21

PinchToZoom ImageView 로 구현

Programming/Android 2013. 8. 23. 15:42 Posted by TanSanC
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

package com.tistory.tansanc.Test130805;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.FloatMath;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ImageView.ScaleType;

public class ImgViewTouch extends ImageView {

	private static final String TAG = "ImgViewChild";
	private static final boolean D = false;

	private Matrix matrix;
	private Matrix savedMatrix;
	private Matrix savedMatrix2;

	private Drawable d;

	private static final int NONE = 0;
	private static final int DRAG = 1;
	private static final int ZOOM = 2;
	private int mode = NONE;

	private PointF start = new PointF();
	private PointF mid = new PointF();
	private float oldDist = 1f;

	private static final int WIDTH = 0;
	private static final int HEIGHT = 1;

	private boolean isInit = false;

	/** Constants describing the state of this imageview */
	private boolean isMoving;
	private boolean isScaling;
	private boolean isRestoring;

	public ImgViewTouch(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		setScaleType(ScaleType.MATRIX);
		matrix = new Matrix();
		savedMatrix = new Matrix();
		savedMatrix2 = new Matrix();
	}

	public ImgViewTouch(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public ImgViewTouch(Context context) {
		this(context, null);
	}

	@Override
	protected void onLayout(boolean changed, int left, int top, int right,
			int bottom) {
		if (D)
			Log.i(TAG, "onLayout");
		d = this.getDrawable();
		super.onLayout(changed, left, top, right, bottom);
		if (isInit == false) {
			init();
			isInit = true;
		}
	}

	@Override
	public void setImageBitmap(Bitmap bm) {
		if (D)
			Log.i(TAG, "setImageBitmap");
		super.setImageBitmap(bm);
		isInit = false;
		init();
	}

	@Override
	public void setImageResource(int resId) {
		if (D)
			Log.i(TAG, "setImageResource");
		super.setImageResource(resId);
		d = getDrawable();
		isInit = false;
		init();
	}

	protected void init() {
		d = this.getDrawable();
		initImgPos();
		setImageMatrix(matrix);
	}

	/**
	 * Sets the image in the imageview using the matrix
	 */
	public void initImgPos() {

		float[] value = new float[9];
		this.matrix.getValues(value);

		int width = this.getWidth();
		int height = this.getHeight();

		if (d == null)
			return;
		int imageWidth = d.getIntrinsicWidth();
		int imageHeight = d.getIntrinsicHeight();
		int scaleWidth = (int) (imageWidth * value[0]);
		int scaleHeight = (int) (imageHeight * value[4]);

		if (imageWidth > width || imageHeight > height) {

			float xratio = (float) width / (float) imageWidth;
			float yratio = (float) height / (float) imageHeight;

			// Math.min fits the image to the shorter axis. (with letterboxes
			// around)
			// Math.max fits the image th the longer axis. (with the other axis
			// cropped)
			value[0] = value[4] = Math.max(xratio, yratio);
		}

		scaleWidth = (int) (imageWidth * value[0]);
		scaleHeight = (int) (imageHeight * value[4]);

		// align the image to the top left corner
		value[2] = 0;
		value[5] = 0;

		// center the image. it will be aligned to the top left corner
		// otherwise.
		value[2] = (float) width / 2 - (float) scaleWidth / 2;
		value[5] = (float) height / 2 - (float) scaleHeight / 2;

		matrix.setValues(value);
		setImageMatrix(matrix);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {

		if (D)
			dumpEvent(event);

		switch (event.getAction() & MotionEvent.ACTION_MASK) {
		case MotionEvent.ACTION_DOWN:
			savedMatrix.set(matrix);
			start.set(event.getX(), event.getY());
			mode = DRAG;
			break;
		case MotionEvent.ACTION_POINTER_DOWN:
			oldDist = spacing(event);
			if (oldDist > 10f) {
				savedMatrix.set(matrix);
				midPoint(mid, event);
				mode = ZOOM;
			}
			break;
		case MotionEvent.ACTION_UP:

		case MotionEvent.ACTION_POINTER_UP:
			mode = NONE;
			restore(matrix);
			break;
		case MotionEvent.ACTION_MOVE:
			if (mode == DRAG) {
				matrix.set(savedMatrix);
				matrix.postTranslate(event.getX() - start.x, event.getY()
						- start.y);

			} else if (mode == ZOOM) {
				float newDist = spacing(event);
				if (newDist > 10f) {
					matrix.set(savedMatrix);
					float scale = newDist / oldDist;
					matrix.postScale(scale, scale, mid.x, mid.y);
				}
			}
			break;
		}

		// Matrix value modification
		// comment out below 2 lines to remove all restrictions on image
		// transformation.
		matrixTuning(matrix);
		setImageMatrix(savedMatrix2);

		return true;
	}

	private float spacing(MotionEvent event) {
		float x = event.getX(0) - event.getX(1);
		float y = event.getY(0) - event.getY(1);
		return FloatMath.sqrt(x * x + y * y);
	}

	private void midPoint(PointF point, MotionEvent event) {
		float x = event.getX(0) + event.getX(1);
		float y = event.getY(0) + event.getY(1);
		point.set(x / 2, y / 2);
	}

	private void matrixTuning(Matrix matrix) {
		float[] value = new float[9];
		matrix.getValues(value);
		float[] savedValue = new float[9];
		savedMatrix2.getValues(savedValue);

		int width = getWidth();
		int height = getHeight();

		Drawable d = getDrawable();
		if (d == null)
			return;
		int imageWidth = d.getIntrinsicWidth();
		int imageHeight = d.getIntrinsicHeight();
		int scaleWidth = (int) (imageWidth * value[0]);
		int scaleHeight = (int) (imageHeight * value[4]);

		// don't let the image go outside
		if (value[2] < width - scaleWidth)
			value[2] = width - scaleWidth;
		if (value[5] < height - scaleHeight)
			value[5] = height - scaleHeight;
		if (value[2] > 0)
			value[2] = 0;
		if (value[5] > 0)
			value[5] = 0;

		// maximum zoom ratio: 2x
		if (value[0] > 2 || value[4] > 2) {
			value[0] = savedValue[0];
			value[4] = savedValue[4];
			value[2] = savedValue[2];
			value[5] = savedValue[5];
		}

		// don't let the image become smaller than the screen
		if (imageWidth > width || imageHeight > height) {
			if (scaleWidth < width && scaleHeight < height) {
				int target = WIDTH;
				if (imageWidth < imageHeight)
					target = HEIGHT;

				if (target == WIDTH)
					value[0] = value[4] = (float) width / imageWidth;
				if (target == HEIGHT)
					value[0] = value[4] = (float) height / imageHeight;

				scaleWidth = (int) (imageWidth * value[0]);
				scaleHeight = (int) (imageHeight * value[4]);

				if (scaleWidth > width)
					value[0] = value[4] = (float) width / imageWidth;
				if (scaleHeight > height)
					value[0] = value[4] = (float) height / imageHeight;
			}
		}

		// don't allow scale down under its size
		else {
			if (value[0] < 1)
				value[0] = 1;
			if (value[4] < 1)
				value[4] = 1;
		}

		// center the image
		scaleWidth = (int) (imageWidth * value[0]);
		scaleHeight = (int) (imageHeight * value[4]);
		if (scaleWidth < width) {
			value[2] = (float) width / 2 - (float) scaleWidth / 2;
		}
		if (scaleHeight < height) {
			value[5] = (float) height / 2 - (float) scaleHeight / 2;
		}

		matrix.setValues(value);
		savedMatrix2.set(matrix);
	}

	/**
	 * Gives animation effect after touchscreen event, puts the image back into
	 * the screen, limits max zoom at specific ratio.
	 */
	private void restore(Matrix m) {

		setImageMatrix(matrix);
	}

	/** Show an event in the LogCat view, for debugging */
	private void dumpEvent(MotionEvent event) {
		String names[] = { "DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE",
				"POINTER_DOWN", "POINTER_UP", "7?", "8?", "9?" };
		StringBuilder sb = new StringBuilder();
		int action = event.getAction();
		int actionCode = action & MotionEvent.ACTION_MASK;
		sb.append("event ACTION_").append(names[actionCode]);
		if (actionCode == MotionEvent.ACTION_POINTER_DOWN
				|| actionCode == MotionEvent.ACTION_POINTER_UP) {
			sb.append("(pid ").append(
					action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
			sb.append(")");
		}
		sb.append("[");
		for (int i = 0; i < event.getPointerCount(); i++) {
			sb.append("#").append(i);
			sb.append("(pid ").append(event.getPointerId(i));
			sb.append(")=").append((int) event.getX(i));
			sb.append(",").append((int) event.getY(i));
			if (i + 1 < event.getPointerCount())
				sb.append(";");
		}
		sb.append("]");
		Log.d(TAG, sb.toString());
	}

}


'Programming > Android' 카테고리의 다른 글

Android onTouch 에서 ACTION_DOWN 만 들어올경우  (0) 2013.10.11
Android 실습 0823  (0) 2013.08.23
Android 채팅 소스 05  (1) 2013.08.21
Android 채팅 소스 04  (0) 2013.08.21
Android 채팅 서버 소스 01  (0) 2013.08.20

Andoid 매트로돔

Programming/Android 2013. 8. 19. 17:21 Posted by TanSanC
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
package com.tistory.tansanc.Test130805;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;

public class MainActivity extends Activity {

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
		mDdok = mPool.load(this, R.raw.ddok, 1);
		MyView mv = new MyView(this, mHandler);
		setContentView(mv);
	}

	Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			mPool.play(mDdok, 1, 1, 0, 0, 1);
		}
	};

	SoundPool mPool;
	int mDdok;

	class MyView extends View {
		Context context;
		Handler mDdokHandler;

		public MyView(Context context, Handler mDdokHandler) {
			super(context);
			this.context = context;
			this.mDdokHandler = mDdokHandler;
			mHandler.sendEmptyMessage(0);
		}

		float degree = 0;
		float inc = 1;

		@Override
		protected void onDraw(Canvas canvas) {
			// TODO Auto-generated method stub

			Paint Pnt = new Paint();
			Pnt.setAntiAlias(true);
			Pnt.setColor(Color.RED);
			Pnt.setStrokeWidth(20);

			canvas.translate(canvas.getWidth() / 2, canvas.getHeight() / 4 * 3);
			if (degree > 45) {
				inc = -1;
				mDdokHandler.sendEmptyMessage(0);
			} else if (degree < -45) {
				inc = 1;
				mDdokHandler.sendEmptyMessage(0);
			}
			canvas.rotate(degree);
			canvas.drawLine(0, 0, 0, -canvas.getHeight() / 2, Pnt);
			super.onDraw(canvas);
		}

		Handler mHandler = new Handler() {
			public void handleMessage(Message msg) {
				degree = degree + inc;
				invalidate();
				mHandler.sendEmptyMessageDelayed(0, 50);
			}
		};
	}
}

'Programming > Android' 카테고리의 다른 글

Android 채팅 소스 01  (0) 2013.08.20
Android Download html  (0) 2013.08.20
Android 시계  (0) 2013.08.19
Andoid 알람 기능으로 라디오 mms 주소로 듣기  (0) 2013.08.16
Android 라디오 mms 주소로 듣기  (1) 2013.08.16

Android 시계

Programming/Android 2013. 8. 19. 16:59 Posted by TanSanC
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
package com.tistory.tansanc.Test130805;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;

public class MainActivity extends Activity {

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		MyView mv = new MyView(this);
		setContentView(mv);
	}
}

class MyView extends View {
	Context context;

	public MyView(Context context) {
		super(context);
		this.context = context;
		mHandler.sendEmptyMessage(0);
	}

	float degree = -30;

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub

		Paint Pnt = new Paint();
		Pnt.setAntiAlias(true);
		Pnt.setColor(Color.YELLOW);
		Pnt.setStrokeWidth(20);
		
		canvas.translate(canvas.getWidth()/2, canvas.getHeight()/2);
		canvas.rotate(degree);
		canvas.drawLine(0, 0, 400, 400, Pnt);
		Pnt.setColor(Color.RED);
		Pnt.setStrokeWidth(10);
		canvas.rotate(degree);
		canvas.drawLine(0, 0, 400, 400, Pnt);
		super.onDraw(canvas);
	}

	Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			degree++;
			invalidate();
			mHandler.sendEmptyMessageDelayed(0, 100);
		}
	};
}

'Programming > Android' 카테고리의 다른 글

Android Download html  (0) 2013.08.20
Andoid 매트로돔  (0) 2013.08.19
Andoid 알람 기능으로 라디오 mms 주소로 듣기  (0) 2013.08.16
Android 라디오 mms 주소로 듣기  (1) 2013.08.16
Android Intro 로딩 Activity  (0) 2013.08.12

Android 라디오 mms 주소로 듣기

Programming/Android 2013. 8. 16. 17:28 Posted by TanSanC
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

package com.tistory.tansanc.Test130805;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends Activity implements OnPreparedListener {

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

	}

	MediaPlayer player;

	public void mOnClick(View v) {

		try {
			Intent it = new Intent(Intent.ACTION_VIEW);
			Uri uri = Uri.parse("mms://114.108.140.39/magicfm_live");
			it.setDataAndType(uri, "video/mp4");
			startActivity(it);
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void onPrepared(MediaPlayer player) {
		Log.d("Start onPrepared", "Start onPrepared");
		player.start();
	}
}

Android Intro 로딩 Activity

Programming/Android 2013. 8. 12. 17:28 Posted by TanSanC
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

public class IntroPage extends Activity
{
	private ProgressDialog pDialog;

	Handler myHandler = new Handler();
	Runnable myRunnable = new Runnable()
	{
		// 이미지 뷰를 띄운 후 2초후에 로그인 페이지로 이동
		public void run()
		{
			pDialog.dismiss();
			Intent i = new Intent(IntroPage.this, Main.class);
			startActivity(i);
			overridePendingTransition(R.anim.fade, R.anim.hold);
			finish();
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.intro);
		pDialog = ProgressDialog.show(this, "", "연결 중....");
		myHandler.postDelayed(myRunnable, 2000);
	}
}
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
package com.tistory.tansanc.Test130805;

import java.util.ArrayList;

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.c09_listview);

		// * 데이터 원본 준비
		ArrayList arContactList = new ArrayList();		
		arContactList = getContactList();
		ArrayList arGeneral = new ArrayList();
		for( int i = 0 ; i < arContactList.size() ; i++ )
		{
			arGeneral.add( arContactList.get(i).name );
		}
		// */

		/*
		 * 배열로 준비 String[] arGeneral = {"김유신", "이순신", "강감찬", "을지문덕"}; //
		 */

		// 어댑터 준비
		ArrayAdapter Adapter;
		Adapter = new ArrayAdapter(this,
				android.R.layout.simple_list_item_1, arGeneral);

		// 어댑터 연결
		ListView list = (ListView) findViewById(R.id.list);
		list.setAdapter(Adapter);
	}

	private ArrayList getContactList() {

		Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;

		String[] projection = new String[] {

		ContactsContract.CommonDataKinds.Phone.CONTACT_ID, // 연락처 ID -> 사진 정보
															// 가져오는데 사용

				ContactsContract.CommonDataKinds.Phone.NUMBER, // 연락처

				ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME }; // 연락처
																		// 이름.

		String[] selectionArgs = null;

		String sortOrder = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME

		+ " COLLATE LOCALIZED ASC";

		Cursor contactCursor = managedQuery(uri, projection, null,

		selectionArgs, sortOrder);

		ArrayList contactlist = new ArrayList();

		if (contactCursor.moveToFirst()) {

			do {

				String phonenumber = contactCursor.getString(1).replaceAll("-",

				"");

				if (phonenumber.length() == 10) {

					phonenumber = phonenumber.substring(0, 3) + "-"

					+ phonenumber.substring(3, 6) + "-"

					+ phonenumber.substring(6);

				} else if (phonenumber.length() > 8) {

					phonenumber = phonenumber.substring(0, 3) + "-"

					+ phonenumber.substring(3, 7) + "-"

					+ phonenumber.substring(7);

				}

				Contact acontact = new Contact();

				acontact.setPhotoid(contactCursor.getLong(0));

				acontact.setPhonenum(phonenumber);

				acontact.setName(contactCursor.getString(2));

				contactlist.add(acontact);

			} while (contactCursor.moveToNext());

		}

		return contactlist;

	}

	public class Contact {
		long photoid;
		String phonenum;
		String name;
		
		public long getPhotoid() {
			return photoid;
		}
		public void setPhotoid(long photoid) {
			this.photoid = photoid;
		}
		public String getPhonenum() {
			return phonenum;
		}
		public void setPhonenum(String phonenum) {
			this.phonenum = phonenum;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		
		
	}


}

Layout XML

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ListView
 android:id="@+id/list" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
 />
</LinearLayout>

 

Manifest 추가

 

<uses-permission android:name="android.permission.READ_CONTACTS" />

'Programming > Android' 카테고리의 다른 글

Android 라디오 mms 주소로 듣기  (1) 2013.08.16
Android Intro 로딩 Activity  (0) 2013.08.12
안드로이드 타자 연습 예제  (0) 2013.08.08
Android 다양한 Layout 사용법  (0) 2013.08.07
Could not find .apk  (0) 2013.05.24

Could not find .apk

Programming/Android 2013. 5. 24. 23:15 Posted by TanSanC
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

Could not find .apk 에러






[Is Library] 를 체크 해제 하면 된다.



'Programming > Android' 카테고리의 다른 글

안드로이드 타자 연습 예제  (0) 2013.08.08
Android 다양한 Layout 사용법  (0) 2013.08.07
앱 시작 액티비티 변경  (0) 2013.05.18
Android SQLite Select Where And &  (0) 2013.05.17
안드로이드 BItmap ImageView Drawable  (0) 2013.05.16

앱 시작 액티비티 변경

Programming/Android 2013. 5. 18. 20:13 Posted by TanSanC
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

앱 시작 액티비티 변경


매니패스트 파일에


<intent-filter>

                <action android:name="android.intent.action.MAIN" />

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

</intent-filter>


요 구문이 들어가있는 액티비티가 앱의 시작 액티비티가 된다.


저 4줄을 원하는 액티비티 내부로 이동시키면 시작 액티비티가 변경 된다.

Android SQLite Select Where And &

Programming/Android 2013. 5. 17. 02:32 Posted by TanSanC
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

안드로이드 환경에서


Select 문 호출시




cursor = db.rawQuery(

"SELECT contents, imagepath FROM diary WHERE month = " + month

+ " And year = " + year + " And day = " + day, null);


& 나 && 대신 And 라고 써야한다....

'Programming > Android' 카테고리의 다른 글

Could not find .apk  (0) 2013.05.24
앱 시작 액티비티 변경  (0) 2013.05.18
안드로이드 BItmap ImageView Drawable  (0) 2013.05.16
카메라 인텐트로 띄운후 해당 이미지 ImageView에 띄우기  (0) 2013.05.16
Intent 정리  (0) 2013.05.09