如何从不同的活动更改视图。

时间:2016-02-06 02:39:59

标签: java android xml

现在我的目标是让Colors活动允许用户点击按钮来改变他们绘制的颜色。如何将我的activity_main.xml中的画布实例添加到我的Colors活动中,以便我可以更改其上绘制的颜色?

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="#09B8C1"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.app.MainActivity">

    <com.app.color.DrawingView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:id="@+id/drawing"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:background="#FFFFFFFF"
        android:layout_above="@+id/settings"
        android:layout_alignParentTop="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="changeColor"
        android:text="@string/Settings"
        android:id="@+id/settings"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Clear"
        android:id="@+id/button5"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:onClick="clearScreen" />
</RelativeLayout>

MainActivity.java

package com.app.color;

import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    DrawingView dv = new DrawingView();

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

    public void changeColor(View view)
    {
        Intent intent = new Intent(MainActivity.this, Colors.class);
        startActivity(intent);
    }

    public void clearScreen(View view)
    {

    }

}

Colors.java

package com.app.color;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class Colors extends AppCompatActivity {

    DrawingView dv = new DrawingView(,);

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

    public void toWhite(View view)
    {
        dv.setColor("#ffffff");
        Intent intent = new Intent(Colors.this, MainActivity.class);
        startActivity(intent);
    }

    public void toYellow(View view)
    {
        dv.setColor("#ffff00");
        Intent intent = new Intent(Colors.this, MainActivity.class);
        startActivity(intent);
    }

    public void toBlue(View view)
    {
        dv.setColor("#0000ff");
        Intent intent = new Intent(Colors.this, MainActivity.class);
        startActivity(intent);
    }

    public void toRed(View view)
    {
        dv.setColor("#ff0000");
        Intent intent = new Intent(Colors.this, MainActivity.class);
        startActivity(intent);
    }

    public void toGreen(View view)
    {
        dv.setColor("#00ff00");
        Intent intent = new Intent(Colors.this, MainActivity.class);
        startActivity(intent);
    }

    public void toBlack(View view)
    {
        dv.setColor("#00000");
        Intent intent = new Intent(Colors.this, MainActivity.class);
        startActivity(intent);
    }
}

DrawingView.java

package com.app.color;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
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 {
    private Paint paint = new Paint();
    private Path path = new Path();

    public DrawingView(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;
            default:
                return false;
        }

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

    public void setColor(String newColor){
        invalidate();
        int paintColor = Color.parseColor(newColor);
        paint.setColor(paintColor);
    }
}

activity_colors.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.app.Colors">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Choose Color"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:layout_below="@+id/textView"
        android:onClick="toWhite"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:background="#ffffff"
        android:text="@string/white" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button2"
        android:layout_below="@+id/button"
        android:onClick="toBlue"
        android:layout_alignRight="@+id/button"
        android:layout_alignEnd="@+id/button"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:background="#153fc7"
        android:text="@string/blue" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/red"
        android:id="@+id/button3"
        android:layout_below="@+id/button2"
        android:onClick="toRed"
        android:layout_alignRight="@+id/button2"
        android:layout_alignEnd="@+id/button2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:background="#f01010" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/yellow"
        android:id="@+id/button4"
        android:background="#f7e80f"
        android:layout_below="@+id/button3"
        android:onClick="toYellow"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignRight="@+id/button3"
        android:layout_alignEnd="@+id/button3" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Green"
        android:id="@+id/button6"
        android:layout_below="@+id/button4"
        android:onClick="toGreen"
        android:layout_alignRight="@+id/button4"
        android:layout_alignEnd="@+id/button4"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:background="#00ff00" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Black"
        android:id="@+id/button7"
        android:layout_below="@+id/button6"
        android:background="#000000"
        android:onClick="toBlack"
        android:textColor="#ffffff"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

我的理解是,您希望在任何事件上更改DrawingView视图的颜色(例如:单击按钮)。

请注意以下代码:

首先,在attrs.xml中创建属性样式

<declare-styleable name="DrawingView">

    <!-- drawing view color attributes -->
    <attr name="dv_background_color" format="color" />
    <attr name="dv_foreground_color" format="color" />

    <!-- You can add other custom attributes also -->
    <attr name="dv_padding" format="dimension" />
    <attr name="dv_width" format="dimension" />
</declare-styleable>

现在在java类文件中编写以下代码:

public class DrawingView extends View {

 //Default colors
 private int mDrawingViewBackgroundColor = 0xFF0000FF;
 private int mDrawingViewForegroundColor = 0xFF0000FF;

 //Default padding
 private int mViewPadding = 5;
 private int mViewWidth = 10;

private Paint mForeGroundColorPaint = new Paint();
private Path mBackGroundColorPaint = new Path();

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

    TypedArray typeArr = context.obtainStyledAttributes(attrs, R.styleable.DrawingView);
  try {

    //Inflate all the attributes of DrawingView from xml that are initialize in activity_main.xml layout file.
    mDrawingViewBackgroundColor = typeArr.getColor(R.styleable.DrawingView_dv_background_color, mDrawingViewBackgroundColor);
    mDrawingViewForegroundColor = (R.styleable.DrawingView_dv_background_color, mDrawingViewForegroundColor);

    mViewPadding = (int)typeArr.getDimension(R.styleable.DrawingView_dv_padding, mViewPadding);

    mViewWidth = (int)typeArr.getDimension(R.styleable.DrawingView_dv_width, mViewWidth);
   }finally {
        typeArr.recycle();
    }

    setupPaints();
}

setupPaints() {
   //Render DrawingView colors - Foreground color
   mForeGroundColorPaint.setColor(mDrawingViewForegroundColor);
   mForeGroundColorPaint.setAntiAlias(true);
   mForeGroundColorPaint.setStrokeWidth(5f);
   mForeGroundColorPaint.setStyle(Paint.Style.STROKE);
   mForeGroundColorPaint.setStrokeJoin(Paint.Join.ROUND);

   //Render DrawingView colors - Background color
   mBackGroundColorPaint.setColor(mDrawingViewBackgroundColor);
   mBackGroundColorPaint.setAntiAlias(true);
   mBackGroundColorPaint.setStrokeWidth(5f);
   mBackGroundColorPaint.setStyle(Paint.Style.STROKE);
   mBackGroundColorPaint.setStrokeJoin(Paint.Join.ROUND);
}

@Override
public void invalidate() {
    setupPaints();
    super.invalidate();
}

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

}