2012-05-03 2 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

게시 드로잉보기 – Akhil

+0

@changweiyao 드로잉 뷰 클래스를 추가했습니다. – RVG

답변

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

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

(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
당신은 사용자 정의 뷰 클래스에 구현하기 위해해야 ​​

로 된 onMeasure을 구현

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


사용자 정의보기의 높이와 너비를 계산해야합니다. 당신은 부모 레이아웃의 중앙 사용자 정의보기에 필요한 경우 는 -을 확인하십시오보기 높이 =

  • 정의 부모보기 높이

및/또는

  • 사용자 정의보기 폭! ! = 상위보기 폭


예를 들어210 :

이제
@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

P.S. android:layout_centerHorizontal="true"은 효과가 없음을 알 수 있습니다. 그 이유는 정확히 너비를 계산하는 대신 부모 폭에있는 측정에 사용하기 때문입니다.

+0

나는 코드를 구현하려고하지만 사용하지는 않는다. 여전히 왼쪽 코어에서만 보여주기 – Ram

관련 문제