생성 된 비트 맵을 클릭 가능하게 만들려면 어떻게합니까? 아래는 캔버스를 사용하여 비트 맵을 만드는 데 사용한 코드입니다.
public class DrawView extends View implements OnClickListener
{
public DrawView(Context context)
{
super(context);
paint = new Paint();
image = BitmapFactory.decodeResource(getResources(), R.drawable.andmrktsmall);
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
int canWidth = canvas.getWidth();
int canHeight = canvas.getHeight();
int width = (canWidth - 200) / 2;
int height = (canHeight - 100) / 2;
Bitmap indexcanvas = Bitmap.createScaledBitmap(image, 200, 100, true);
canvas.drawBitmap(indexcanvas, width, height, paint);
this.setBackgroundColor(Color.YELLOW);
}
@Override
public void onClick(View v)
{
Toast.makeText(context, "View is clicked", 1).show();
}
}
이보기에 OnClickListener를 설정하면 모든보기를 클릭 할 수 있습니다 (비트 맵에 국한되지는 않음). 사용자가 비트 맵 자체 만 클릭했는지 여부를 확인하려면 onTouchEvent (MotionEvent 이벤트)를 재정의하고 터치 좌표가 비트 맵과 동일한 지 확인해야합니다.
@Override
public boolean onTouchEvent(MotionEvent event)
{
float x = event.getX();
float y = event.getY();
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
//Check if the x and y position of the touch is inside the bitmap
if( x > bitmapXPosition && x < bitmapXPosition + bitmapWidth && y > bitmapYPosition && y < bitmapYPosition + bitmapHeight )
{
//Bitmap touched
}
return true;
}
return false;
}
bitmapXPosition 및 bitmapYPosition을 비트 맵을 그리는 데 사용하는 좌표로 바꾸고, bitmapWidth 및 bitmapHeight를 그리는 데 사용하는 너비와 높이로 바꾸십시오.
또한 모든 뷰의 onDraw () 메서드 내에 메모리를 할당 (객체 생성)하지 마십시오. 성능에 좋지 않습니다.
편집하다
private Rect r;
private Paint paint;
Bitmap bitmap;
public TestRect(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
paint = new Paint();
paint.setColor(Color.BLUE);
r = new Rect();
bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
}
public TestRect(Context context, AttributeSet attrs) {
super(context, attrs);
paint = new Paint();
paint.setColor(Color.BLUE);
r = new Rect();
bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
}
public TestRect(Context context) {
super(context);
paint = new Paint();
paint.setColor(Color.BLUE);
r = new Rect();
bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
}
@Override
public void onDraw(Canvas c)
{
r.set(getWidth()/2, getHeight()/2, getWidth()/2 + 200, getHeight()/2 + 200);
//c.drawRect(r, paint);
c.drawBitmap(bitmap, null, r, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
float x = event.getX();
float y = event.getY();
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
//Check if the x and y position of the touch is inside the bitmap
if( x > getWidth()/2 && x < getWidth()/2 + 200 && y > getHeight()/2 && y < getHeight()/2 + 200 )
{
Log.e("TOUCHED", "X: " + x + " Y: " + y);
//Bitmap touched
}
return true;
}
return false;
}
Rect를 좌표로 사용하여 비트 맵을 그리면 터치가 비트 맵 내부에 있는지 여부를 확인할 수 있습니다. 또한 크고보기 흉한 "if"문 대신 다음을 사용할 수 있습니다.
if(r.contains(x, y))
{
//BITMAP TOUCHED
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다