【问题标题】:need help Custom View with multiple buttons in Android需要帮助在Android中使用多个按钮自定义视图
【发布时间】:2014-04-25 04:03:08
【问题描述】:

到目前为止,我通过扩展 ImageView 类完成了自定义视图。

现在,

我必须为屏幕上的绘图和 2 个按钮创建自定义视图。

我必须分开显示按钮和自定义视图。按钮不应超过自定义视图。

怎么做..?

需要你的帮助............

我的示例代码..

package com.androiddom.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.ImageView;

public class CustomView extends ImageView {

     private Paint paint = new Paint();
     private Path path = new Path();

     public CustomView(Context context) {
         super(context);

         paint.setAntiAlias(true);
         paint.setColor(Color.GREEN);
         paint.setStyle(Paint.Style.STROKE);
         paint.setStrokeJoin(Paint.Join.ROUND);
         paint.setStrokeWidth(15f);

     }

     @Override
     protected void onDraw(Canvas canvas) {
         super.onDraw(canvas);       

         canvas.drawPath(path, paint);       
     }


    @Override
    public boolean onTouchEvent(MotionEvent event) {

        // Gives you x and y coordinates on the Event.
        float pointX = event.getX();
        float pointY = event.getY();

        // Checks for the event that occurs
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            path.moveTo(pointX, pointY);

            return true;
        case MotionEvent.ACTION_MOVE:
            path.lineTo(pointX, pointY);             

            break;

        case MotionEvent.ACTION_UP:
             XYZ etc...

            break;
        default:
            return false;
        }


        postInvalidate();
        return true;
    }     

public void clear() {
        //Path path = new Path();
        path.reset();
        invalidate();
    }
}

目前我正在从主要活动中调用 customview 类,如以下方法:

MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    CustomeView tv = (CustomeView) 
    setContentView(tv);

    }
}

【问题讨论】:

  • 我必须创建绘图应用程序,触摸手指线将被绘制..

标签: android view android-canvas android-view android-custom-view


【解决方案1】:

您尝试使用 XML 布局如下...

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="10dip"
            android:layout_weight="1"
            android:text="Button 1" />

        <Button
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="10dip"
            android:layout_weight="1"
            android:text="Button 2" />
    </LinearLayout>

    <com.androiddom.customview
        android:id="@+id/custom_view"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_margin="20dip"
        android:layout_weight="1" >
    </com.androiddom.customview>

</LinearLayout>

更新:

假设,上面的 XML 布局名称 activity_main.xml。现在,更新MainActivity.java如下...

MainActivity extends Activity implements OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        // Initialize your CustomeView object in this way 
        // then do what you want to do with this object
        CustomeView tv = (CustomeView) findViewById(R.id.custom_view);

        Button bt = (Button)findViewById(R.id.button);
        btPrevious.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) { 

        tv.clear();

    }

}

另一个更新:

CustomeView.java 类中创建一个用于清除绘图视图的新方法,如下所示...

// start new drawing
public void startNew() {
    canvas.drawColor(0, PorterDuff.Mode.CLEAR);
    invalidate();
}

然后在onClick()里面调用这个方法...

@Override
public void onClick(View v) { 

    tv.startNew();

}

您可以从DrawingView找到更多关于自定义绘图的帮助

【讨论】:

  • 我试过了,但只显示了客户视图。你能告诉我从主要活动如何调用这个客户视图类吗?使用 XML 进行排列。
  • 嘿,还有一个帮助 plz ..,我想通过单击按钮清除视图。我正在向客户视图添加 CLEAR 方法:在 onclick 中调用该方法时,什么也没发生。视图不清楚。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-18
  • 1970-01-01
  • 2014-08-11
  • 1970-01-01
  • 2018-07-02
  • 1970-01-01
相关资源
最近更新 更多