2012-05-03 75 views
2

的中心我寫了一個自定義的視圖類。自定義視圖顯示在屏幕

我已經實現在主要佈局在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> 

在圖形佈局中,它在中心位置上正確顯示。但是當我在模擬器上執行時,它顯示在左上角。

我試圖改變以編程方式實現佈局,使用下面的代碼。

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

但仍是它在左上角的核心人。

我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; 

    } 

} 

是什麼問題?以及如何在屏幕中心顯示自定義視圖。 ?

+0

與DrawingView發佈您的代碼。 –

+0

post DrawingView – Akhil

+0

@changweiyao我已經添加了繪圖視圖類 – RVG

回答

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

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

實現onMeasure(INT,INT)

+0

我試圖實現代碼,但沒有用。仍然只在左邊的核心展示。 – RVG

+0

我試着用我的計算機。有用。我的意思是XML方式不是代碼方式。 –

+0

我試過了,它完美地工作。檢查完整的例子在這裏: https://github.com/shanwu/shanwu_coding_base/tree/SimplePieChartExample – shanwu

0

改變它

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

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

我試着實現代碼但沒用。仍然只在左邊的核心展示。 – Ram

0

你應該在你的自定義視圖類來實現

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


在這裏面,你應該計算的高度和寬度自定義視圖。 如果您需要在父佈局中央您的自定義視圖 - 確保:

  • 自定義視圖高度=父視圖高度

AND/OR

  • 自定義視圖寬度! !=父視圖寬度


例如:

@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" 

enter image description here

附:你看到android:layout_centerHorizontal="true"不起作用。原因是在onMeasure代替的計算精確寬度使用父寬度

+0

我嘗試執行代碼但沒用。仍然只在左邊的核心展示 – Ram