【问题标题】:Add Draggable Imageview to relative layout from custom class android从自定义类 android 将 Draggable Imageview 添加到相对布局
【发布时间】:2014-11-26 05:09:49
【问题描述】:

如何从扩展相对布局的自定义类中将图像视图添加到特定位置的相对布局,并使该图像视图可拖动到布局的特定部分?
下面是我要在其中添加图像的自定义类。
那么,我应该在画布视图上添加图像还是仅仅在相对布局上添加图像?
请给我一个合适的解决方案。

public class CustomCompassView extends RelativeLayout {

int width;
int height;
int saparationRatio;

int captureRange;

private ImageView mImage;

public CustomCompassView(Context context) {
    super(context);
}

public CustomCompassView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public CustomCompassView(Context context, AttributeSet attrs) {
    super(context, attrs);

    mImage = (ImageView) getChildAt(2);
}

@Override
protected void onDraw(Canvas canvas) {

    super.onDraw(canvas);

    width = this.getWidth();
    height = this.getHeight();
    saparationRatio = (width / 2) / 5;

    Log.e(AppConstant.LOG_TAG, "height: " + height + " & width: " + width);

    drawCircleAbove125(canvas);

}

protected void drawCircleAbove125(Canvas canvas) {

    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setColor(Color.BLACK);
    paint.setStyle(Paint.Style.FILL);
    paint.setStrokeWidth(0.0f);
    canvas.drawCircle(width / 2, height / 2, width / 2, paint);


}

【问题讨论】:

    标签: android android-layout drag-and-drop draggable android-custom-view


    【解决方案1】:

    在你的相对布局类中膨胀一个布局

    LayoutInflater inflater = LayoutInflater.from (context);
    View v = inflater.inflate (R.layout.some_file, null, true);
    
    this.addView (v);
    

    您可以在膨胀的布局中添加任何您喜欢的内容。

    【讨论】:

    • 是的,但我想在特定 (x,y) 位置上添加图像视图,并希望它也可拖动。我该如何实现?
    • 您需要使用边界计算来做到这一点。您可以在此处阅读有关拖放的更多信息developer.android.com/guide/topics/ui/drag-drop.html
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-12
    • 1970-01-01
    相关资源
    最近更新 更多