简体   繁体   English

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

[英]Custom View show on center of the screen

i write a custom view class. 我写了一个自定义视图类。

i've implement the custom view on Main Layout xml. 我在Main Layout xml上实现了自定义视图。 and set parameter for center of the screen using below code 并使用以下代码设置屏幕中心的参数

<?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>

In graphics layout , it show correctly on center postion. 在图形布局中,它在中心位置正确显示。 But when i execute on simulator, it shows on left top corner. 但是当我在模拟器上执行时,它显示在左上角。

i've tried to change to implement the layout programmatically, using below code. 我试图改变以编程方式实现布局,使用下面的代码。

RelativeLayout layout = new RelativeLayout(this);
        DrawingView  myView = new DrawingView(this);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
        layout.addView(myView,params);
        setContentView(linearLayout);

But still its show on left top corener. 但它仍然是左顶级核心的表现。

My drawingview class is, 我的绘图视图是,

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;

    }

}

what is the problem? 问题是什么? and how to show the custom view on center of the screen. 以及如何在屏幕中心显示自定义视图。 ?

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

    this.setMeasuredDimension(backgound.getWidth(), backgound.getHeight());
}

implement the onMeasure(int,int) 实现onMeasure(int,int)

change it 更改

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

to

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

You should to implement in your custom View class 您应该在自定义View类中实现

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


In it you should calculate height and width for your custom View. 在其中,您应该计算自定义视图的高度和宽度。 If you need to central your custom View in parent layout - make sure that: 如果您需要在父布局中集中自定义视图 - 请确保:

  • custom View height != parent View height 自定义查看高度!=父视图高度

AND/OR AND / OR

  • custom View width != parent View width 自定义视图宽度!=父视图宽度


For example: 例如:

@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);
    }


Now you can use next for your custom View: 现在,您可以使用next作为自定义视图:

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

在此输入图像描述

PS You see that android:layout_centerHorizontal="true" gives no effect. PS你看到android:layout_centerHorizontal="true"没有效果。 The reason is using parent width in onMeasure instead of calculating exact width. 原因是在onMeasure中使用父宽度而不是计算精确宽度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM