gpt4 book ai didi

android - 如何在android中为位于 Canvas 中心的项目放置一个onclick事件?

转载 作者:太空狗 更新时间:2023-10-29 15:08:56 24 4
gpt4 key购买 nike

我已经实现了以下代码,以在我使用 Canvas 放置项目的圆形滚动中获取项目。我得到这样的布局。 Circular scroll 我需要在到达 Canvas 中心的项目上设置一个 onclick 监听器,并显示一条消息,其中将包含图像的详细信息。我如何确定到达中心的图像并在其上设置点击监听器???? onclick 应该只在中心项目上而不是其余项目上。

有人可以帮我解决这个问题吗???

提前致谢!

public class CircleScrollListView extends SurfaceView implements
SurfaceHolder.Callback, OnGestureListener {
private GestureDetector mGestureDetector;
private Thread mThread;
private ArrayList<CircleDrawItem> datas = new ArrayList<CircleDrawItem>();
int[] playerDrawableResourceIds = new int[] { R.drawable.ronaldo,
R.drawable.zindance, R.drawable.congvinh, R.drawable.huynhduc,
R.drawable.gerrard, R.drawable.nagatomo, R.drawable.messi,
R.drawable.minhphuong, R.drawable.neymar, R.drawable.ronaldo_beo,
R.drawable.ronaldinho, R.drawable.xavi };
public int mCenterX;
public int mCenterY;
public int mRadius;
public double mStartAngleInRadian = Math.PI / 4;
private boolean isStop = false;

public CircleScrollListView(Context context, AttributeSet attrs) {
super(context, attrs);
mGestureDetector = new GestureDetector(context, this);
getHolder().addCallback(this);
this.setFocusable(true);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
mGestureDetector.onTouchEvent(event);
return true;
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}

public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {

// Calculate ratios of height and width to requested height and
// width
final int heightRatio = Math.round((float) height
/ (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);

// Choose the smallest ratio as inSampleSize value, this will
// guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}

return inSampleSize;
}

public static Bitmap decodeSampledBitmapFromResource(Resources res,
int resId, int reqWidth, int reqHeight) {

// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);

// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}

private double calculateScrollAngle(float px1, float py1, float px2,
float py2) {
double radian1 = Math.atan2(py1, px1);
double radian2 = Math.atan2(py2, px2);
double diff = radian2 - radian1;
return diff;
}

@Override
public void surfaceCreated(SurfaceHolder holder) {

setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));

Global.density = getResources().getDisplayMetrics().density;
Global.dw = getResources().getDisplayMetrics().widthPixels;
Global.dh = getResources().getDisplayMetrics().heightPixels;
Global.dp = Global.density / 1.5f;

// For circle data
mCenterX = (int) (Global.dw / 2.0f);
mCenterY = (int) (Global.dh);
// mCenterX = (int) (Global.dp * 200);
// mCenterY = (int) (Global.dh / 2.0f);
mRadius = (int) (300 * Global.dp);
mStartAngleInRadian = Math.PI / 4;

for (int i = 0; i < playerDrawableResourceIds.length; i++) {
CircleDrawItem circleDrawItem = new CircleDrawItem();
circleDrawItem.mIconBitmap = decodeSampledBitmapFromResource(
getResources(), playerDrawableResourceIds[i], 50, 50);
circleDrawItem.mAngle = mStartAngleInRadian +i * Math.PI / 10;
datas.add(circleDrawItem);
}

mThread = new Thread(new Runnable() {
@Override
public void run() {
while (!isStop) {
draw();
}
}
});

mThread.start();
}

protected void draw() {
Canvas canvas = getHolder().lockCanvas();
if (canvas == null) {
return;
}
canvas.save();
canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setAntiAlias(true);

for (int i = 0; i < datas.size(); i++) {
canvas.save();
CircleDrawItem item = datas.get(i);
double x = mCenterX + Math.cos(item.mAngle) * mRadius;
double y = mCenterY - Math.sin(item.mAngle) * mRadius;
canvas.drawBitmap(item.mIconBitmap,
(int) x - item.mIconBitmap.getWidth() / 2, (int) y
- item.mIconBitmap.getHeight() / 2, paint);
canvas.restore();
}
canvas.restore();
getHolder().unlockCanvasAndPost(canvas);
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
isStop = true;
}

@Override
public boolean onDown(MotionEvent e) {
return false;
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
return false;
}

@Override
public void onLongPress(MotionEvent e) {

}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
float tpx = e1.getY();
float tpy = e2.getX();
float disx = (int) distanceY;
float disy = (int) distanceX;
double scrollAngle = calculateScrollAngle(tpx, tpy, tpx + disx, tpy
+ disy);
for (int i = 0; i < datas.size(); i++) {
datas.get(i).mAngle += scrollAngle;
}
return true;
}

@Override
public void onShowPress(MotionEvent e) {
}

@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
}

最佳答案

我们开始了;我没有那么多 Java 经验,但我应该能够帮助您了解如何去做。

在您的代码中创建一个带有 posX、posY、宽度和高度的小正方形。也尝试获取图像坐标。现在比较他们两个。图片是否与您的小方 block 位于同一位置?

你明白这背后的原因吗?

关于android - 如何在android中为位于 Canvas 中心的项目放置一个onclick事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18822465/

24 4 0
文章推荐: html - 将