2014-03-28 3 views
4

이 색상을 사용하고 싶습니다. https://code.google.com/p/android-color-picker/ pikcerdialog 무엇을해야할지 모르겠습니다. 이전에 외부 항아리와 물건을 추가했지만 프로젝트를 다운로드 할 때 다른 경우가 있습니다. 나는 안드로이드에 새로운 것이 아니라 여전히 비교적 새로운 것입니다. 그 전체 프로젝트가 도서관인가 뭔가. 나는 정말로 어디에서 시작해야할지 모르지만 실제로 도움을 원할 것입니다. 튜토리얼에 대한 링크조차도 좋을 것이다. 왜냐하면 나는 무엇을 검색해야할지 모르기 때문이다.색상 선택기 이해하기 대화 상자

package com.example.color; 

    import android.app.AlertDialog; 
    import android.content.Context; 
    import android.content.DialogInterface; 
    import android.content.SharedPreferences; 
    import android.content.pm.PackageInfo; 
    import android.content.pm.PackageManager.NameNotFoundException; 
    import android.preference.PreferenceManager; 
    import android.text.Html; 
    import android.view.LayoutInflater; 
    import android.view.View; 
    import android.widget.ImageView; 
    import android.widget.TextView; 
    import android.widget.Toast; 

public class AboutDialog extends AlertDialog { 

private ImageView      mIconView; 
private TextView      mAppNameText; 
private TextView      mAboutText; 
private TextView      mVersionText; 


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

     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View layout = inflater.inflate(R.layout.dialog, null); 

     mAboutText = (TextView) layout.findViewById(android.R.id.text2); 
     mVersionText = (TextView) layout.findViewById(android.R.id.text1); 
     mAppNameText = (TextView) layout.findViewById(android.R.id.title); 
     mIconView = (ImageView) layout.findViewById(android.R.id.icon); 

     setView(layout); 

     loadAbout(); 

     setTitle("About"); 



     mIconView.setOnClickListener(new View.OnClickListener() { 

       int mClickCount = 0; 

       @Override 
       public void onClick(View v) { 
         mClickCount++; 

         if(mClickCount == 5) { 
           Toast.makeText(getContext(), "Upgraded to Pro Version!", Toast.LENGTH_SHORT).show(); 

           new Thread(new Runnable() { 

             @Override 
             public void run() { 
               SharedPreferences.Editor edit = PreferenceManager.getDefaultSharedPreferences(getContext()).edit();              
               edit.putBoolean("is_pro", true); 
               edit.commit(); 
             } 

           }).start(); 


         } 

       } 
     }); 



     setButton(DialogInterface.BUTTON_POSITIVE, getContext().getString(android.R.string.ok), new DialogInterface.OnClickListener() { 

       @Override 
       public void onClick(DialogInterface dialog, int which) { 
         dialog.dismiss(); 
       } 
     }); 

} 

private void loadAbout(){ 

     PackageInfo pi = null; 
     try { 
       pi = getContext().getPackageManager().getPackageInfo(getContext().getPackageName(), 0); 
     } catch (NameNotFoundException e) { 
       e.printStackTrace(); 
     } 

     mAppNameText.setText("ColorPickerView");   
     mVersionText.setText("Version" + " " + (pi != null ? pi.versionName : "null")); 

     String s = "<b>Developed By:</b><br>Daniel Nilsson<br>";     
     mAboutText.setText(Html.fromHtml(s)); 

} 

} 

대화이었고 이것은 내가이은 여러분의 필요에 수정 사용

+0

점검이 같은 리스너를 통해 대화를 할 수 있습니다 http://stackoverflow.com/questions/18552290/different-text-color-for-each-class-object/18552365#18552365 – JRowan

+0

나는 그것을 따라 갔고 나는 그것을 실행할 때 initaite를 할 수 없다고 말한다. 빈 생성자가 없습니다. – user3450719

+0

코드가 무엇인지 알 수 있습니까? – JRowan

답변

6

환영}

package com.example.color; 

    import com.example.color.ColorPickerDialog.OnColorChangedListener; 

    import android.os.Bundle; 
    import android.app.Activity; 
    import android.graphics.Color; 
    import android.graphics.Paint; 
    import android.view.Menu; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
     import android.widget.Button; 

     public class MainActivity extends Activity implements OnColorChangedListener,    OnClickListener { 

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


    Button b = (Button) findViewById(R.id.button10); 
    b.setOnClickListener(this); 

} 

@Override 
public void colorChanged(String key, int color) { 
    // TODO Auto-generated method stub 
    // Paint l = Paint.setColor(color); 
} 

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

@Override 
public void onClick(View arg0) { 
    // TODO Auto-generated method stub 
    ColorPickerDialog color = new ColorPickerDialog(this,this, "picker",Color.BLACK,Color.WHITE); 
    color.show(); 
} 

에서 호출 할 경우 그냥 정말 간단 활동 즉

import android.app.Dialog; 
import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.LinearGradient; 
import android.graphics.Paint; 
import android.graphics.Shader; 
import android.os.Bundle; 
import android.view.MotionEvent; 
import android.view.View; 

public class ColorPickerDialog extends Dialog { 

public interface OnColorChangedListener { 
    void colorChanged(String key, int color); 
} 

private OnColorChangedListener mListener; 
private int mInitialColor, mDefaultColor; 
private String mKey; 

private class ColorPickerView extends View { 
    private Paint mPaint; 
    private float mCurrentHue = 0; 
    private int mCurrentX = 0, mCurrentY = 0; 
    private int mCurrentColor, mDefaultColor; 
    private final int[] mHueBarColors = new int[258]; 
    private int[] mMainColors = new int[65536]; 
    private OnColorChangedListener mListener; 
    private int width,height; 

    ColorPickerView(Context c, OnColorChangedListener l, int color, 
      int defaultColor) { 
     super(c); 
     mListener = l; 
     mDefaultColor = defaultColor; 

     // Get the current hue from the current color and update the main 
     // color field 
     float[] hsv = new float[3]; 
     Color.colorToHSV(color, hsv); 
     mCurrentHue = hsv[0]; 
     updateMainColors(); 

     mCurrentColor = color; 

     // Initialize the colors of the hue slider bar 
     int index = 0; 
     for (float i = 0; i < 256; i += 256/42) // Red (#f00) to pink 
                // (#f0f) 
     { 
      mHueBarColors[index] = Color.rgb(255, 0, (int) i); 
      index++; 
     } 
     for (float i = 0; i < 256; i += 256/42) // Pink (#f0f) to blue 
                // (#00f) 
     { 
      mHueBarColors[index] = Color.rgb(255 - (int) i, 0, 255); 
      index++; 
     } 
     for (float i = 0; i < 256; i += 256/42) // Blue (#00f) to light 
                // blue (#0ff) 
     { 
      mHueBarColors[index] = Color.rgb(0, (int) i, 255); 
      index++; 
     } 
     for (float i = 0; i < 256; i += 256/42) // Light blue (#0ff) to 
                // green (#0f0) 
     { 
      mHueBarColors[index] = Color.rgb(0, 255, 255 - (int) i); 
      index++; 
     } 
     for (float i = 0; i < 256; i += 256/42) // Green (#0f0) to yellow 
                // (#ff0) 
     { 
      mHueBarColors[index] = Color.rgb((int) i, 255, 0); 
      index++; 
     } 
     for (float i = 0; i < 256; i += 256/42) // Yellow (#ff0) to red 
                // (#f00) 
     { 
      mHueBarColors[index] = Color.rgb(255, 255 - (int) i, 0); 
      index++; 
     } 

     // Initializes the Paint that will draw the View 
     mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 
     mPaint.setTextAlign(Paint.Align.CENTER); 
     mPaint.setTextSize(12); 
    } 

    // Get the current selected color from the hue bar 
    private int getCurrentMainColor() { 
     int translatedHue = 255 - (int) (mCurrentHue * 255/360); 
     int index = 0; 
     for (float i = 0; i < 256; i += 256/42) { 
      if (index == translatedHue) 
       return Color.rgb(255, 0, (int) i); 
      index++; 
     } 
     for (float i = 0; i < 256; i += 256/42) { 
      if (index == translatedHue) 
       return Color.rgb(255 - (int) i, 0, 255); 
      index++; 
     } 
     for (float i = 0; i < 256; i += 256/42) { 
      if (index == translatedHue) 
       return Color.rgb(0, (int) i, 255); 
      index++; 
     } 
     for (float i = 0; i < 256; i += 256/42) { 
      if (index == translatedHue) 
       return Color.rgb(0, 255, 255 - (int) i); 
      index++; 
     } 
     for (float i = 0; i < 256; i += 256/42) { 
      if (index == translatedHue) 
       return Color.rgb((int) i, 255, 0); 
      index++; 
     } 
     for (float i = 0; i < 256; i += 256/42) { 
      if (index == translatedHue) 
       return Color.rgb(255, 255 - (int) i, 0); 
      index++; 
     } 
     return Color.RED; 
    } 

    // Update the main field colors depending on the current selected hue 
    private void updateMainColors() { 
     int mainColor = getCurrentMainColor(); 
     int index = 0; 
     int[] topColors = new int[256]; 
     for (int y = 0; y < 256; y++) { 
      for (int x = 0; x < 256; x++) { 
       if (y == 0) { 
        mMainColors[index] = Color.rgb(
          255 - (255 - Color.red(mainColor)) * x/255, 
          255 - (255 - Color.green(mainColor)) * x/255, 
          255 - (255 - Color.blue(mainColor)) * x/255); 
        topColors[x] = mMainColors[index]; 
       } else 
        mMainColors[index] = Color.rgb(
          (255 - y) * Color.red(topColors[x])/255, 
          (255 - y) * Color.green(topColors[x])/255, 
          (255 - y) * Color.blue(topColors[x])/255); 
       index++; 
      } 
     } 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     int translatedHue = 255 - (int) (mCurrentHue * 255/360); 
     // Display all the colors of the hue bar with lines 
     for (int x = 0; x < 256; x++) { 
      // If this is not the current selected hue, display the actual 
      // color 
      if (translatedHue != x) { 
       mPaint.setColor(mHueBarColors[x]); 
       mPaint.setStrokeWidth(1); 
      } else // else display a slightly larger black line 
      { 
       mPaint.setColor(Color.BLACK); 
       mPaint.setStrokeWidth(3); 
      } 
      canvas.drawLine(x + 10, 0, x + 10, 40, mPaint); 
      // canvas.drawLine(0, x+10, 40, x+10, mPaint); 
      //canvas.drawLine(x + width/27, 0, x + width/27,height/8 , mPaint); 
     } 

     // Display the main field colors using LinearGradient 
     for (int x = 0; x < 256; x++) { 
      int[] colors = new int[2]; 
      colors[0] = mMainColors[x]; 
      colors[1] = Color.BLACK; 
      Shader shader = new LinearGradient(0, 50, 0, 306, colors, null, 
        Shader.TileMode.REPEAT);//0,50,0,306 
      mPaint.setShader(shader); 
      canvas.drawLine(x + 10, 50, x + 10, 306, mPaint); 
      //canvas.drawLine(x + width/27, height/8 + 10, x + width/27, height*(5/6), mPaint); 
     } 
     mPaint.setShader(null); 

     // Display the circle around the currently selected color in the 
     // main field 
     if (mCurrentX != 0 && mCurrentY != 0) { 
      mPaint.setStyle(Paint.Style.STROKE); 
      mPaint.setColor(Color.BLACK); 
      canvas.drawCircle(mCurrentX, mCurrentY, 10, mPaint); 
     } 

     // Draw a 'button' with the currently selected color 
     mPaint.setStyle(Paint.Style.FILL); 
     mPaint.setColor(mCurrentColor); 
     //canvas.drawRect(10, 316, 138, 356, mPaint); 
     //canvas.drawRect(width/27, height*(5/6), width/2, height, mPaint); 

     // Set the text color according to the brightness of the color 
     if (Color.red(mCurrentColor) + Color.green(mCurrentColor) 
       + Color.blue(mCurrentColor) < 384) 
      mPaint.setColor(Color.WHITE); 
     else 
      mPaint.setColor(Color.BLACK); 
     // canvas.drawText(
       // "New Color", 74, 
       //340, mPaint); 

     // Draw a 'button' with the default color 
     mPaint.setStyle(Paint.Style.FILL); 
     mPaint.setColor(mDefaultColor); 
     //canvas.drawRect(138, 316, 266, 356, mPaint); 
     //canvas.drawRect(width/2, height*(5/6), width, height, mPaint); 

     // Set the text color according to the brightness of the color 
     if (Color.red(mDefaultColor) + Color.green(mDefaultColor) 
       + Color.blue(mDefaultColor) < 384) 
      mPaint.setColor(Color.WHITE); 
     else 
      mPaint.setColor(Color.BLACK); 
     //canvas.drawText(
       // "Default Color", 202, 340, 
       //mPaint); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     setMeasuredDimension(276,366);//276,366 
     width = widthMeasureSpec; 
     height = heightMeasureSpec; 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     if (event.getAction() != MotionEvent.ACTION_DOWN) 
      return true; 
     float x = event.getX(); 
     float y = event.getY(); 

     // If the touch event is located in the hue bar 
     if (x > 10 && x < 266 && y > 0 && y < 40) { 
      // Update the main field colors 
      mCurrentHue = (255 - x) * 360/255; 
      updateMainColors(); 

      // Update the current selected color 
      int transX = mCurrentX - 10; 
      int transY = mCurrentY - 60; 
      int index = 256 * (transY - 1) + transX; 
      if (index > 0 && index < mMainColors.length) 
       mCurrentColor = mMainColors[256 * (transY - 1) + transX]; 

      // Force the redraw of the dialog 
      invalidate(); 
     } 

     // If the touch event is located in the main field 
     if (x > 10 && x < 266 && y > 50 && y < 306) { 
      mCurrentX = (int) x; 
      mCurrentY = (int) y; 
      int transX = mCurrentX - 10; 
      int transY = mCurrentY - 60; 
      int index = 256 * (transY - 1) + transX; 
      if (index > 0 && index < mMainColors.length) { 
       // Update the current color 
       mCurrentColor = mMainColors[index]; 
       mListener.colorChanged("", mCurrentColor); 
       // Force the redraw of the dialog 
       invalidate(); 
      } 
     } 

     // If the touch event is located in the left button, notify the 
     // listener with the current color 
     //if (x > 10 && x < 138 && y > 316 && y < 356) 
      // mListener.colorChanged("", mCurrentColor); 

     // If the touch event is located in the right button, notify the 
     // listener with the default color 
     //if (x > 138 && x < 266 && y > 316 && y < 356) 
      // mListener.colorChanged("", mDefaultColor); 

     return true; 
    } 
} 

public ColorPickerDialog(Context context, OnColorChangedListener listener, 
     String key, int initialColor, int defaultColor) { 
    super(context); 

    mListener = listener; 
    mKey = key; 
    mInitialColor = initialColor; 
    mDefaultColor = defaultColor; 
} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    OnColorChangedListener l = new OnColorChangedListener() { 
     public void colorChanged(String key, int color) { 
      mListener.colorChanged(mKey, color); 
      dismiss(); 
     } 
    }; 

    setContentView(new ColorPickerView(getContext(), l, mInitialColor, 
      mDefaultColor)); 
    setTitle("Pick Text Color"); 

    } 
} 

활동에서 사용법 편집

최초의 메이크업 활동 후

implement OnColorChangedListener 

당신은 콜백 함수를

@Override 
    public void colorChanged(String key, int color) { 
     // TODO Auto-generated method stub 
     paint.setColor(color); 
    } 

를 얻을 코드에서 아웃이

ColorPickerDialog color = new ColorPickerDialog(this,this, "picker",Color.BLACK,Color.WHITE); 
      color.show(); 
+0

더 이상이 스레드에 관심을 기울이지 않는 사람이 있습니까? 내가 그것을 발견했기 때문에 나는 일하는 것을 얻었습니다. 유일한 문제는 높은 rez 장치에서 전혀 확장되지 않는다는 것입니다. 누구든지이 솔루션을 가지고 있습니까? – durbnpoisn

+0

작은 해결책이지만, 많은 "마법의 숫자"를 사용하며 색조 막대의 크기와 관련하여 작업이 명확하지 않습니다. – Dale