我写了一个自定义视图类.
我在Main Layout 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);
但它仍然是左顶级核心的表现.
我的绘图视图是,
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; }}
问题是什么?以及如何在屏幕中心显示自定义视图. ?
解决方法:
@OverrIDeprotected voID onMeasure(int wIDthMeasureSpec, int heightmeasureSpec) { this.setMeasuredDimension(backgound.getWIDth(), backgound.getHeight());}
实现onMeasure(int,int)
总结以上是内存溢出为你收集整理的android – 自定义视图显示在屏幕中央全部内容,希望文章能够帮你解决android – 自定义视图显示在屏幕中央所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)