如何添加颜色选择器中的应用? [英] How to add Color Picker in Application?

查看:142
本文介绍了如何添加颜色选择器中的应用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了一个应用程序,它使用的屏幕为板岩和手指作为一支粉笔,这是正常工作。但是,我想用不同的颜色类型粉笔。

I have developed an Application , which uses screen as a Slate and finger as a Chalk, which is working properly. But I want to use different color types for chalk.

下面是我的code:

MyDemo.java

    package com.example.mydemo;
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.View;
    import android.widget.Button;
    import android.widget.LinearLayout;


    public class MyDemo extends Activity {

    private LinearLayout root;
    private Button btnReset;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_demo);

        root = (LinearLayout) findViewById(R.id.root);
        btnReset = (Button) findViewById(R.id.reset);

        final MyImageView view = new MyImageView(this);
        view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
        root.addView(view);

        btnReset.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                view.reset();
            }
        });
    }

}

MyImageView.java

package com.example.mydemo;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import java.util.ArrayList;
import java.util.List;

public class MyImageView extends ImageView implements View.OnTouchListener {

    private int id = -1;
    private Path path;
    private List<Path> paths = new ArrayList<Path>();
    private List<PointF> points = new ArrayList<PointF>();

    boolean multiTouch = false;

    public MyImageView(Context context) {
        super(context);
        this.setOnTouchListener(this);
    }

    public void reset() {
        paths.clear();
        points.clear();
        path = null;
        id = -1;
        invalidate();
    }

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

        Paint paint = createPen(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        for (Path path : paths) {
            canvas.drawPath(path, paint);
        }

        for (int i = 0; i < points.size(); i++) {
            PointF p = points.get(i);
            canvas.drawText("" + p.x, p.y, i, createPen(Color.WHITE));
        }
    }

    private PointF copy(PointF p) {
        PointF copy = new PointF();
        copy.set(p);
        return copy;
    }

    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction();
        switch (action & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                multiTouch = false;

                id = event.getPointerId(0);
                PointF p = getPoint(event, id);
                path = new Path();
                path.moveTo(p.x, p.y);
                paths.add(path);

                points.add(copy(p));
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                multiTouch = true;
                for (int i = 0; i < event.getPointerCount(); i++) {
                    int tId = event.getPointerId(i);
                    if (tId != id) {
                        points.add(getPoint(event,i));
                    }
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (!multiTouch) {
                    p =getPoint(event, id);
                    path.lineTo(p.x, p.y);
                }
                break;
        }

        invalidate();

        return true;
    }

    private PointF getPoint(MotionEvent event, int i) {
        int index = 0;
        return new PointF(event.getX(index), event.getY(index));
    }

    private Paint createPen(int color) {
        Paint pen = new Paint();
        pen.setColor(color);
        float width = 3;
        pen.setStrokeWidth(width);
        return pen;
    }

}

activity_my_demo.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/root" xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >

    <Button android:id="@+id/reset" android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Reset"
            />
</LinearLayout>

任何一个可以告诉我什么应该被添加或在我的code改变,这样我可以用不同颜色的粉笔?

Can any one tell me what should be added or changed in my code so that I can use different colors for Chalk?

推荐答案

您可以把在样品中的Andr​​oid文件夹的。

You can take the sample in your android folder's..

有关颜色选择器到这个文件夹中:

For the color picker go to in this folder:

安卓/样本/ Android的YOURS_VERSION / ApiDemos

android/samples/android-YOURS_VERSION/ApiDemos

这篇关于如何添加颜色选择器中的应用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆