【问题标题】:How to add Color Picker in Application?如何在应用程序中添加颜色选择器?
【发布时间】:2012-09-07 11:03:56
【问题描述】:

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

这是我的代码:

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>

谁能告诉我应该在我的代码中添加或更改什么以便我可以为粉笔使用不同的颜色?

【问题讨论】:

    标签: android android-layout android-widget touch color-picker


    【解决方案1】:

    您可以在您的 android 文件夹中获取样本..

    对于颜色选择器,请转到此文件夹:

    android/samples/android-YOURS_VERSION/ApiDemos

    【讨论】:

      【解决方案2】:

      使用这个,查看这个代码。比你的问题得到解决

            package com.example.changecolor;
            import android.app.Activity;
            import android.content.pm.PackageInfo;
            import android.content.pm.PackageManager;
            import android.graphics.Color;
            import android.os.Bundle;
            import android.view.Menu;
            import android.view.MenuItem;
            import android.view.View;
            import android.widget.Button;
            import android.widget.LinearLayout;
            import android.widget.TextView;
      
      
       public class MainActivity extends Activity
        implements View.OnClickListener, UberColorPickerDialog.OnColorChangedListener {
      
      private int mColor = 0xFFFF0000;
      
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      
          //Write the version number
          PackageManager pm = getPackageManager();
          String versionName = "";
          try {
              PackageInfo pi = pm.getPackageInfo("com.keithwiley.android.ubercolorpickerdemo", 0);
              versionName = pi.versionName;
          }
          catch (Exception e) {
          }
          TextView textView = (TextView) findViewById(R.id.version);
          textView.setText(versionName);
      
          //Initialize the sample
          ((LinearLayout) findViewById(R.id.LinearLayout)).setBackgroundColor(mColor);
          float hsv[] = new float[3];
          Color.colorToHSV(mColor, hsv);
          if (UberColorPickerDialog.isGray(mColor))
              hsv[1] = 0;
          if (hsv[2] < .5)
              ((TextView) findViewById(R.id.sample)).setTextColor(Color.WHITE);
          else ((TextView) findViewById(R.id.sample)).setTextColor(Color.BLACK);
      
          //Set up the buttons
          Button button = (Button) findViewById(R.id.colorPickerWithTitle);
          button.setOnClickListener(this);
      
          button = (Button) findViewById(R.id.colorPickerWithToast);
          button.setOnClickListener(this);
      }
      
      protected void onPause() {
          super.onPause();
      }
      
      protected void onResume() {
          super.onResume();
      }
      
      public boolean onCreateOptionsMenu(Menu menu) {
          super.onCreateOptionsMenu(menu);
      
          return true;
      }
      
      public boolean onPrepareOptionsMenu(Menu menu) {
          super.onPrepareOptionsMenu(menu);
      
          return true;
      }
      
      public boolean onOptionsItemSelected(MenuItem item) {
          switch (item.getItemId()) {
          }
      
          return super.onOptionsItemSelected(item);
      }
      
      public void onClick(View v) {
          if (v.getId() == R.id.colorPickerWithTitle)
              new UberColorPickerDialog(this, this, mColor, true).show();
          if (v.getId() == R.id.colorPickerWithToast)
              new UberColorPickerDialog(this, this, mColor, false).show();
      }
      
      public void colorChanged(int color) {
          ((LinearLayout) findViewById(R.id.LinearLayout)).setBackgroundColor(mColor=color);
      
          float hsv[] = new float[3];
          Color.colorToHSV(mColor, hsv);
          //if (UberColorPickerDialog.isGray(mColor))
          //  hsv[1] = 0;
          if (hsv[2] < .5)
              ((TextView) findViewById(R.id.sample)).setTextColor(Color.WHITE);
          else ((TextView) findViewById(R.id.sample)).setTextColor(Color.BLACK);
         }
        }
      

      并使用颜色选择器类

      【讨论】:

        猜你喜欢
        • 2014-03-25
        • 2010-11-08
        • 2012-04-09
        • 1970-01-01
        • 2015-09-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-06
        相关资源
        最近更新 更多