Android清晰视图使用按钮

时间:2015-06-17 02:21:55

标签: java android

免责声明:我对android / java来说相当新。

我按照教程:http://creative-punch.net/2014/03/make-basic-single-touch-drawing-app-android/添加了几个按钮。我可以让按钮显示祝酒词,所以按钮正常工作,但我不知道如何让一个按钮访问MainDrawingView类,让它清除视图以摆脱以前的绘图。

这是MainActivity类:

package com.mycompany.simpledraw;

import android.content.Context;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import org.w3c.dom.Attr;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    public void clearButtonClicked (View view){
        Toast.makeText(this, "Clear clicked!", Toast.LENGTH_SHORT).show();
    }

    public void submitButtonClicked (View view){
        Toast.makeText(this, "Submit clicked!", Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

这是MainDrawingView类:

package com.mycompany.simpledraw;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.ArrayList;

public class MainDrawingView extends View {
    private Paint paint = new Paint();
    private Path path = new Path();
    LineData LD = new LineData();

    public MainDrawingView(Context context, AttributeSet attrs) {
        super(context, attrs);

        paint.setAntiAlias(true);
        paint.setStrokeWidth(5f);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawPath(path, paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // Get the coordinates of the touch event
        float eventX = event.getX();
        float eventY = event.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // Set a new starting point
                path.moveTo(eventX, eventY);
                return true;
            case MotionEvent.ACTION_MOVE:
                // Connect the points
                path.lineTo(eventX, eventY);
                break;
            case MotionEvent.ACTION_UP:
                break;
            default:
                return false;
        }

        // Makes our view repaint and call onDraw
        invalidate();
        return true;
    }

}

这是activity_main.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"
    android:background="#ffffff"
    tools:context="com.example.app.FullscreenActivity">

    <!-- This is the view on which we will draw. -->
    <view
        android:layout_width="match_parent"
        android:layout_height="0dp"
        class="com.mycompany.simpledraw.MainDrawingView"
        android:id="@+id/single_touch_view"
        android:layout_gravity="start|top"
        android:layout_weight="1"
        android:background="#ffffff" />

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

        <Button
            android:id="@+id/clear_btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/clear_btn"
            android:onClick="clearButtonClicked" />

        <Button
            android:id="@+id/submit_btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/submit_btn"
            android:onClick="submitButtonClicked"/>

    </LinearLayout>


</LinearLayout>

2 个答案:

答案 0 :(得分:1)

在按钮的处理程序中,您可以使用Activity的findViewById方法找到视图。

 public void clearButtonClicked (View view){
     MainDrawingView mainDrawingView = (MainDrawingView)findViewById(R.id.single_touch_view);

     // Now you can call methods on mainDrawingView
 }

注意 R.id.id.single_touch_view 匹配您在activity_main.xml中为视图指定的ID

在下面的评论中,您提出了第二个问题 - 如何清除视图。我的建议来自以下内容:Android, canvas: How do I clear (delete contents of) a canvas (= bitmaps), living in a surfaceView?

1)在MainDrawingView类中,添加实例变量

private boolean forceClear;

2)在MainDrawingView类中,添加以下方法

public void clearView() {
    forceClear = true;
    invalidate();  // this will force a call to onDraw
}

3)在MainDrawingView类中,按如下所示更新onDraw方法。

protected void onDraw(Canvas canvas) {
    if (forceClear) {
        canvas.drawColor(Color.BLACK);  // or whatever color works best for you.
        forceClear = false;
    } else {
        canvas.drawPath(path, paint);
    }
}

4)然后在你的按钮处理程序中,只需调用新方法:

public void clearButtonClicked (View view){
         MainDrawingView mainDrawingView = (MainDrawingView)findViewById(R.id.single_touch_view);
         mainDrawingView.clearView();
 }

答案 1 :(得分:0)

let rec AppendManually list1 list2= 
  match list1,list2 with
  | [],l | l,[] -> l
  | x::list1', y::list2' -> 
     if x < y then x :: (AppendManually list1' list2) 
     else y :: (AppendManually list1 list2')