Android 색상 선택 디스크 구현

8453 단어 Android색상 선택
본 논문 의 사례 는 안 드 로 이 드 가 색상 선택 원반 의 구체 적 인 코드 를 공유 하여 여러분 께 참고 하 실 수 있 습 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.
효과 도 를 먼저 보다

xml 레이아웃

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:orientation="vertical" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 > 
<TextView 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:text="@string/hello" 
 android:id="@+id/tv_rgb"/> 
<RelativeLayout android:id="@+id/relativeLayout1" android:layout_height="fill_parent" android:layout_width="fill_parent"> 
 
 <com.myview.ColorPickerView 
  android:id="@+id/cpv" 
  android:layout_width="230dp" 
  android:layout_height="230dp" 
  android:layout_centerInParent="true" 
  android:scaleType="center" 
  android:src="@drawable/rgb" /> 
 
</RelativeLayout> 
</LinearLayout> 
ColorPickerView 색상 선택 원반

package com.myview; 
 
import android.annotation.SuppressLint; 
import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.PointF; 
import android.graphics.drawable.BitmapDrawable; 
import android.util.AttributeSet; 
import android.view.MotionEvent; 
import android.widget.ImageView; 
 
public class ColorPickerView extends ImageView { 
 Context context; 
 private Bitmap iconBitMap; 
 float iconRadius;//        
 float iconCenterX; 
 float iconCenterY; 
 PointF iconPoint;//        
 
 public ColorPickerView(Context context) { 
  this(context, null); 
 } 
 
 public ColorPickerView(Context context, AttributeSet attrs, int defStyle) { 
  super(context, attrs, defStyle); 
  this.context = context; 
  init(); 
 } 
 
 public ColorPickerView(Context context, AttributeSet attrs) { 
  this(context, attrs, 0); 
  init(); 
 } 
 
 Paint mBitmapPaint; 
 Bitmap imageBitmap; 
 float viewRadius;//   view   
 float radius;//      
 
 /** 
  *       
  */ 
 private void init() { 
  iconBitMap = BitmapFactory.decodeResource(context.getResources(), 
    R.drawable.pickup);//       
  iconRadius = iconBitMap.getWidth() / 2;//         
 
  mBitmapPaint = new Paint(); 
  iconPoint = new PointF(); 
 
  imageBitmap = ((BitmapDrawable) getDrawable()).getBitmap(); 
  radius = imageBitmap.getHeight() / 2;//      
 
  // //     
  iconPoint.x = radius; 
  iconPoint.y = radius; 
 
 } 
 
 @Override 
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
  // TODO Auto-generated method stub 
  super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
 } 
 
 Canvas canvas; 
 
 @Override 
 protected void onDraw(Canvas canvas) { 
  // TODO Auto-generated method stub 
  super.onDraw(canvas); 
  this.canvas = canvas; 
 
  viewRadius = this.getWidth() / 2;//   view   
 
  canvas.drawBitmap(iconBitMap, iconPoint.x - iconRadius, iconPoint.y 
    - iconRadius, mBitmapPaint); 
 } 
 
 @SuppressLint("ClickableViewAccessibility") 
 @Override 
 public boolean onTouchEvent(MotionEvent event) { 
  float x = event.getX(); 
  float y = event.getY(); 
  int pixel; 
  int r; 
  int g; 
  int b; 
  switch (event.getAction()) { 
  case MotionEvent.ACTION_MOVE: 
   proofLeft(x, y); 
   pixel = getImagePixel(iconPoint.x, iconPoint.y); 
   r = Color.red(pixel); 
   g = Color.green(pixel); 
   b = Color.blue(pixel); 
   if (mChangedListener != null) { 
    mChangedListener.onMoveColor(r, g, b); 
   } 
   if (isMove) { 
    isMove = !isMove; 
    invalidate(); 
   } 
   break; 
  case MotionEvent.ACTION_UP: 
   pixel = getImagePixel(iconPoint.x, iconPoint.y); 
   r = Color.red(pixel); 
   g = Color.green(pixel); 
   b = Color.blue(pixel); 
   if (mChangedListener != null) { 
    mChangedListener.onColorChanged(r, g, b); 
   } 
   break; 
 
  default: 
   break; 
  } 
  return true; 
 } 
 
 public int getImagePixel(float x, float y) { 
 
  Bitmap bitmap = imageBitmap; 
  //        
  int intX = (int) x; 
  int intY = (int) y; 
  if (intX < 0) 
   intX = 0; 
  if (intY < 0) 
   intY = 0; 
  if (intX >= bitmap.getWidth()) { 
   intX = bitmap.getWidth() - 1; 
  } 
  if (intY >= bitmap.getHeight()) { 
   intY = bitmap.getHeight() - 1; 
  } 
  int pixel = bitmap.getPixel(intX, intY); 
  return pixel; 
 
 } 
 
 /** 
  * R = sqrt(x * x + y * y) 
  * point.x = x * r / R + r 
  * point.y = y * r / R + r 
  */ 
 private void proofLeft(float x, float y) { 
 
  float h = x - viewRadius; //  xy           
  float w = y - viewRadius;//  xy           
  float h2 = h * h; 
  float w2 = w * w; 
  float distance = (float) Math.sqrt((h2 + w2)); //            
  if (distance > radius) { //           ,            x,y 
   float maxX = x - viewRadius; 
   float maxY = y - viewRadius; 
   x = ((radius * maxX) / distance) + viewRadius; //              x,y 
   y = ((radius * maxY) / distance) + viewRadius; 
  } 
  iconPoint.x = x; 
  iconPoint.y = y; 
 
  isMove = true; 
 } 
 
 boolean isMove; 
 
 public void setOnColorChangedListenner(OnColorChangedListener l) { 
  this.mChangedListener = l; 
 } 
 
 private OnColorChangedListener mChangedListener; 
 
 //           rgb  
 public interface OnColorChangedListener { 
  //     ,       
  void onColorChanged(int r, int g, int b); 
 
  //         
  void onMoveColor(int r, int g, int b); 
 } 
} 
MyViewActivity 메 인 인터페이스

package com.myview; 
 
import com.myview.ColorPickerView.OnColorChangedListener; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TextView; 
import android.widget.Toast; 
 
public class MyViewActivity extends Activity { 
  
 TextView tv_rgb; 
  
 /** Called when the activity is first created. */ 
 @Override 
 public void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.main); 
   
  tv_rgb=(TextView)this.findViewById(R.id.tv_rgb); 
   
  ColorPickerView cpv=(ColorPickerView)this.findViewById(R.id.cpv); 
  cpv.setOnColorChangedListenner(new OnColorChangedListener() { 
   /** 
    *     ,      
    */ 
   @Override 
   public void onColorChanged(int r, int g, int b) { 
    if(r==0 && g==0 && b==0){ 
     return; 
    } 
    Toast.makeText(MyViewActivity.this, "   RGB:"+r+","+g+","+b, Toast.LENGTH_SHORT).show(); 
   } 
 
   /** 
    *         
    */ 
   @Override 
   public void onMoveColor(int r, int g, int b) { 
    if(r==0 && g==0 && b==0){ 
     return; 
    } 
    tv_rgb.setText("RGB:"+r+","+g+","+b); 
   } 
  }); 
 } 
} 
세부 항목 코드:
원본 다운로드:Android 색상 선택 디스크 구현
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기