首页 文章

如何在布局中的特定位置添加视图(在java类中描述)?

提问于
浏览
0

这是我的xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout0"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CanvasActivity"
android:baselineAligned="false">

<LinearLayout
    android:id="@+id/LinearLayout1"
    android:layout_width="wrap_content"
    android:layout_height="match_parent" 
    android:layout_weight="1"
    android:background="#f9dfcb">  
</LinearLayout>

<LinearLayout
    android:id="@+id/LinearLayout2"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="3"
    android:background="#000000" 
    >


</LinearLayout>

</LinearLayout>

我的活动代码

import android.app.Activity;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
import android.widget.Toast;

public class CanvasActivity extends Activity {

private GestureDetector gestureDetector;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_canvas);
    gestureDetector = new GestureDetector(this, new GestureListener());



    LinearLayout LinearLayout1=(LinearLayout)findViewById(R.id.LinearLayout1);
    LinearLayout1.setOnTouchListener(new OnTouchListener(){
        public boolean onTouch(View v, MotionEvent event){
            if (gestureDetector.onTouchEvent(event)) 
            {

                return true;
            }
            return false;
        }
    });


/*********EDITED*******/

    LinearLayout LinearLayout2=(LinearLayout)findViewById(R.id.LinearLayout2);
    int width=LinearLayout2.getWidth();
    int height=LinearLayout2.getHeight();
    LinearLayout2.addView(new Canvasview(this), width, height)  ;

    LinearLayout2.setOnTouchListener(new OnTouchListener(){
        public boolean onTouch(View v, MotionEvent event){

            Canvasview cv= new Canvasview(v.getContext());
            cv.onTouchEvent(event);

            return true;
        }
    });
}


//discerning swipes
    class GestureListener extends SimpleOnGestureListener
    {

        private static final int SWIPE_MIN_DISTANCE = 120;
        private static final int SWIPE_THRESHOLD_VELOCITY = 200;

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2,
                                        float velocityX, float velocityY) 
        {
             if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE &&
                     Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            //From Right to Left
             Toast.makeText(CanvasActivity.this, "Left Swipe", Toast.LENGTH_SHORT).show();


                  return true;           


        } 
             else if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE &&
                    Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
            //From Bottom to Top
            Toast.makeText(CanvasActivity.this, "Top Swipe", Toast.LENGTH_SHORT).show();


            return true;
        }

            return false;
        }
        @Override
        public boolean onDown(MotionEvent e) 
        {

            return true;
        }
    }

}

我的Canvasview看起来像这样

public class Canvasview extends SurfaceView implements SurfaceHolder.Callback{

private int width, height;
private Paint textPaint = new Paint();
private Paint touchPaint = new Paint();
private int colors[] = new int[10];

public Canvasview(Context context) {
    super(context);
    SurfaceHolder holder = getHolder();
    holder.addCallback(this);

    setFocusable(true); // make sure we get key events
    setFocusableInTouchMode(true); // make sure we get touch events
    init();
}

private void init() 
{
    textPaint.setColor(Color.WHITE);
    colors[0] = Color.RED;


    touchPaint = new Paint();
    touchPaint.setColor(colors[0]);

}

public boolean onTouchEvent(MotionEvent event) 
{
    Canvas c = getHolder().lockCanvas();

    if (c != null) 
    {

        if (event.getAction() == MotionEvent.ACTION_UP) 
        {
            // clear everything

        }
        else 
        {
            int xval = (int)event.getX();
            int yval = (int)event.getY();
            drawCircle(xval,yval,touchPaint,c);
        }

        getHolder().unlockCanvasAndPost(c);
    }



    return true;

}


public void clear(Canvas c){
    c = getHolder().lockCanvas();
    c.drawColor(Color.BLACK);
    getHolder().unlockCanvasAndPost(c);

}


private void drawCircle(int x, int y, Paint paint, Canvas c) 
{
    c.drawCircle(x, y, 2, paint);

}



@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){

    Canvas c = getHolder().lockCanvas();




    if (c != null) 
    {
        // clear screen
        c.drawColor(Color.BLACK);

        getHolder().unlockCanvasAndPost(c);
    }
}

@Override
public void surfaceCreated(SurfaceHolder arg0) {
    // TODO Auto-generated method stub

}

@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
    // TODO Auto-generated method stub

}

}

我只需要在linearlayout2中使用画布,但画布占据了整个屏幕 . 我不知道我哪里出错了 .

编辑:嗯,我做了一些变化,我在线性布局中得到了Canvasview,Canvasview的ontouchevents检测到触摸事件,但在Canvasview中,public void surfaceChanged(SurfaceHolder holder,int format,int width,int height)方法没有初始化画布,因此它总是为空 . 要绘制我需要画布不为空 . 任何帮助表示赞赏

3 回答

  • 0

    为第一个线性布局设置layout_width的硬编码值,然后重试

  • 0

    第一个问题是你想获得一个尚未在onCreate()中绘制的视图的高度和宽度:

    int width=LinearLayout2.getWidth();
    int height=LinearLayout2.getHeight();
    

    可能的解决方案是设置静态值或使用ViewTreeObserver(https://stackoverflow.com/search?q=get+measured+width+height+android) .

    您可能遇到的另一个问题是线性布局不会调用onTouch()方法 . 您可以尝试使用滚动视图(例如scrollview)而不是线性布局 .

  • 0

    我改变了我的xml文件

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout0"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".CanvasActivity"
    android:baselineAligned="false">
    
    
     <View
        android:id="@+id/view0"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" 
        android:layout_weight="3"
        android:background="#f9dfcb"
    
    />
    <com.multitel.testwidget.Canvasview
                    xmlns:android="http://schemas.android.com/apk/res/android" 
                    android:id="@+id/surfaceView1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1">                  
    </com.multitel.testwidget.Canvasview>
    
    
    </LinearLayout>
    

    并将Canvasview中的构造函数更改为

    public Canvasview(Context context, AttributeSet attrs) 
    {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        SurfaceHolder holder = getHolder();
        holder.addCallback(this);
        setFocusable(true); // make sure we get key events
        setFocusableInTouchMode(true); // make sure we get touch events
        init();
        holder.getSurface();
    }
    

    我可以在画布上画画!!

相关问题