package com.gusfree.draw;
import java.io.InputStream;
import java.net.URL;
import java.util.Random;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
public class DrawActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
WindowManager mWindowManager =
(WindowManager)getSystemService(WINDOW_SERVICE);
Display mDisplay = mWindowManager.getDefaultDisplay();
Log.i("phoneInfo", "mDisplay.getWidth()>>> "+mDisplay.getWidth());
Log.i("phoneInfo", "mDisplay.getHeight()>>> "+mDisplay.getHeight());
// View - TextView
}
class MyView extends View{
public MyView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint yellow=new Paint();
yellow.setColor(Color.YELLOW);
yellow.setAntiAlias(true);
canvas.drawCircle(160, 50, 20, yellow);
yellow.setStyle(Style.STROKE);
canvas.drawCircle(160+100, 50,20, yellow);
yellow.setStrokeWidth(10); //선 두껍게
canvas.drawLine(160+20, 50+50,
160+100-20, 50+50, yellow);
/* mission : 동그라미 100개 random한 크기와 위치 */
Random random = new Random();
Paint rPaint=new Paint();
//rPaint.setStyle(Style.STROKE);
for(int i=0 ; i < 100 ; i++ ){
int x = random.nextInt(320); // 0~320까지 랜덤
int y = random.nextInt(400);
int r = random.nextInt(160);
int red=random.nextInt(255);
int green=random.nextInt(255);
int blue=random.nextInt(255);
rPaint.setColor(Color.argb(80, red, green, blue)); //0~255
canvas.drawCircle(x, y, r, rPaint);
}
Paint bluePaint=new Paint();
bluePaint.setColor(Color.BLUE);
canvas.drawRect(100, 200,100+200, 200+50,bluePaint);
RectF oval=new RectF(50, 50, 300, 100); //네모
canvas.drawOval(oval, bluePaint); //타원
Paint white=new Paint();
white.setColor(Color.WHITE);
white.setTextSize(30);
canvas.drawText("안녕하세요", 75, 75, white);
canvas.drawArc(oval,
-360, 100, false, white);
String addr="http://www.imaeil.com/news_img/2012/StarNews/20120504_165031000.jpg";
try {
URL url = new URL(addr); // String -> URL
//단말기에서 WebServer로 데이터 요청 + Permission
InputStream is = url.openStream(); //요청 + 결과 받기
//비트맵 공장에서 받은 데이터로 Bitmap 생성
Bitmap bitmap=BitmapFactory.decodeStream(is);
//비트맵을 지정된 위치에 그리기
//canvas.drawBitmap(bitmap, 10, 10, null);
//비트맵을 지정된 위치에 지정된 크기로 그리기
Rect src=new Rect(0, 0, //원래 크기
0+bitmap.getWidth(),
0+bitmap.getHeight());
//원하는 위치와 크기
RectF dst=new RectF(0, 200, 160, 360);
canvas.drawBitmap(bitmap, null, dst, null);
} catch (Exception e) {
}
}
}
}
package com.gusfree.draw;
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.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
public class DrawActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
WindowManager mWindowManager =
(WindowManager)getSystemService(WINDOW_SERVICE);
Display mDisplay = mWindowManager.getDefaultDisplay();
Log.i("phoneInfo", "mDisplay.getWidth()>>> "+mDisplay.getWidth());
Log.i("phoneInfo", "mDisplay.getHeight()>>> "+mDisplay.getHeight());
// View - TextView
}
class MyView extends View{
public MyView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint white=new Paint();
white.setColor(Color.WHITE);
white.setTextSize(30);
canvas.drawText("안녕하세요", 75, 75, white);
}
//옵션메뉴 -> onOptionsItemSelected()
//on: 콜백,자동,항상 ,TouchEvent
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.i("정보보기",event.getAction()+"");
Log.i("onTouchEvent",event.getEventTime()+"");
Log.w("onTouchEvent","X의 위치 :" +event.getRawX()+"");
Log.w("onTouchEvent","Y의 위치 :" +event.getRawY()+"");
Log.i("onTouchEvent",""+event.getDeviceId()+"");
return super.onTouchEvent(event);
}
}
}
화면을 클릭한 부분의 좌표값이 나온다...로그캣으로...
package com.gusfree.draw;
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.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;
public class DrawActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
WindowManager mWindowManager =
(WindowManager)getSystemService(WINDOW_SERVICE);
Display mDisplay = mWindowManager.getDefaultDisplay();
Log.i("phoneInfo", "mDisplay.getWidth()>>> "+mDisplay.getWidth());
Log.i("phoneInfo", "mDisplay.getHeight()>>> "+mDisplay.getHeight());
// View - TextView
}
class MyView extends View{
public MyView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint white=new Paint();
white.setColor(Color.WHITE);
white.setTextSize(30);
canvas.drawText("안녕하세요", 75, 75, white);
}
//옵션메뉴 -> onOptionsItemSelected()
//on: 콜백,자동,항상 ,TouchEvent
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.i("정보보기",event.getAction()+"");
Log.i("onTouchEvent",event.getEventTime()+"");
Log.w("onTouchEvent","X의 위치 :" +event.getRawX()+"");
Log.w("onTouchEvent","Y의 위치 :" +event.getRawY()+"");
Log.i("onTouchEvent",""+event.getDeviceId()+"");
//터치 다운의 이벤트만 처리하기
if(event.getAction()==MotionEvent.ACTION_DOWN){
if(event.getRawX()>75&&event.getRawX()<75+100&&
event.getRawY()>75&&event.getRawY()<75+100){
Toast.makeText(DrawActivity.this, "안녕하세요", 0).show();
}
}
return super.onTouchEvent(event);
}
}
}
안녕하세요 를 클릭시 다이얼로그가 뜬다~~~(폰테스트시 터치임)