Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 自定义视图显示在屏幕中央_Android - Fatal编程技术网

Android 自定义视图显示在屏幕中央

Android 自定义视图显示在屏幕中央,android,Android,我编写了一个自定义视图类 我已经在主布局xml上实现了自定义视图。并使用下面的代码设置屏幕中心的参数 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_paren

我编写了一个自定义视图类

我已经在主布局xml上实现了自定义视图。并使用下面的代码设置屏幕中心的参数

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
     android:background="@drawable/cs_background"
     android:id="@+id/layout"
     >

   <com.drawings.DrawingView
            android:id="@+id/drawingview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"  
            /> 

 </RelativeLayout>
但它仍然显示在左上角的corener

我的drawingview课程是

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class DrawingView extends View
{
    Context context;
    private Path mPath; 
    private Bitmap backgound;
    private Paint mPaint;
    private float mX,mY;

    private static final float TOUCH_TOLERANCE =4;


    public DrawingView(Context c)
    {
        super(c);
        context         = c;
        init();
    }
    public DrawingView (Context c, AttributeSet as)
    {
        super(c,as);
        context         = c;
        init();
    }

    private void init() 
    {
        mPath       = new Path();
        mPaint      = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(0xFFFF0000);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(12);
        backgound   = BitmapFactory.decodeResource(context.getResources(), R.drawable.cs_one);
    }

    @Override
    protected void onDraw(Canvas canvas) 
    {
        canvas.drawBitmap(backgound, 0, 0, null);
        if(!mPath.isEmpty())
            canvas.drawPath(mPath, mPaint);

        invalidate();
    }

    private void touch_start(float x, float y) 
    {
        mPath.moveTo(x, y);

    }
    private void touch_move(float x, float y) 
    {
        float dx = Math.abs(x - mX);
        float dy = Math.abs(y - mY);

        if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE)
        {
            mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) 
    {
        float x = event.getX();
        float y = event.getY();

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            touch_start(x, y);
            break;
        case MotionEvent.ACTION_MOVE:
            touch_move(x, y);
            break;
        case MotionEvent.ACTION_UP:
//          touch_up();
            break;
        }
        invalidate();
        return true;

    }

}
有什么问题?以及如何在屏幕中央显示自定义视图

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    this.setMeasuredDimension(backgound.getWidth(), backgound.getHeight());
}
实现onMeasure(int,int)

更改它

 RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);


您应该在自定义视图类中实现

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

在其中,您应该计算自定义视图的高度和宽度。 如果需要在父布局中集中自定义视图,请确保:

  • 自定义视图高度!=父视图高度
和/或

  • 自定义视图宽度!=父视图宽度

例如:

@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        final int height = mCircleRadius * 2; //calculated
        final int width = getMeasuredWidth(); //parent width

        setMeasuredDimension(width, height);
    }

现在,您可以在自定义视图中使用“下一步”:

android:layout_centerHorizontal="true"
android:layout_centerVertical="true"


另外,您可以看到,
android:layout\u centerHorizontal=“true”
没有任何效果。原因是在onMeasure中使用了父级宽度,而不是计算精确的宽度。

在DrawingView中发布代码。@changweiyao我添加了DrawingView类。您应该自己实现onMeasure(int,int)。试试这个:params.addRule(RelativeLayout.CENTER\u in\u parent,0)@zazgmy我试着放0。但并没有到达中心位置。它仍然只显示在左上角。我试图实现代码,但没有用。它仍然只在左边的取样器上显示。我用我的计算机尝试了一下。它起作用了。我的意思是XML方式而不是代码方式。我试过了,而且效果很好。检查这里的完整示例:我也尝试实现代码,但没有任何用处。它仍然只在左边显示corer我试图实现代码,但没有用。它仍然只在左边显示corer我试图实现代码,但没有用。仍然只在左边的取芯器上显示。
@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        final int height = mCircleRadius * 2; //calculated
        final int width = getMeasuredWidth(); //parent width

        setMeasuredDimension(width, height);
    }
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"