26 Şubat 2018 Pazartesi

Color Sınıfı

Giriş
Şu satırı dahil ederiz.
import android.graphics.Color;
argb metodu
Şöyle yaparız. alpha rengin ne kadar mat (opak) olduğunu belirtir.
Color.argb(255, 255, 255, 255)
FF yüzde yüz mat demektir.
100%  FF
95%  F2
90%  E6
85%  D9
80%  CC
75%  BF
70%  B3
65%  A6
60%  99
55%  8C
50%  80
45%  73
40%  66
35%  59
30%  4D
25%  40
20%  33
15%  26
10%  1A
5%  0D
0%  00
BLACK alanı
Şöyle yaparız.
Color.BLACK ; Color.WHITE;
blue metodu
Şöyle yaparız.
int color = ...;
int b = Color.blue(color)
colorToHSV metodu
Şöyle yaparız. RGB'den HSV renk uzayına geçisi sağlar.
/**
 * @param bitmap which bitmap to convert
 * @param hue any value from 0 to 360.
 * @return bitmap with new hue values
 */
public Bitmap setHue(Bitmap bitmap, float hue){
    Bitmap newBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
    int width = newBitmap.getWidth();
    int height = newBitmap.getHeight();
    float [] hvs = new float[3];

    for(int y = 0; y < height; y++){
        for(int x = 0; x < width; x++){
            int pixel = newBitmap.getPixel(x,y);
            Color.colorToHSV(pixel,hvs);
            hvs[0] = hue;
            newBitmap.setPixel(x,y,Color.HSVToColor(Color.alpha(pixel),hvs));
        }
    }
    return newBitmap;
}
green metodu
Şöyle yaparız.
int color = ...;
int g = Color.green(color)
parseColor metodu
Açıklaması şöyle
Supported formats are: #RRGGBB #AARRGGBB or one of the following names: 'red', 'blue', 'green', 'black', 'white', 'gray', 'cyan', 'magenta', 'yellow', 'lightgray', 'darkgray', 'grey', 'lightgrey', 'darkgrey', 'aqua', 'fuchsia', 'lime', 'maroon', 'navy', 'olive', 'purple', 'silver', 'teal'.
Şöyle yaparız.
Color.parseColor("#1AFFFFFF")
Şöyle yaparız.
String name = "yellow";
Color.parseColor(name);
red metodu
Şöyle yaparız.
int color = ...;
int r = Color.red(color)
rgb metodu
Şöyle yaparız.
int d = 0;
int color = Color.rgb(d, d, d);
setAlpha metodu
Şöyle yaparız.
textView.getBackground().setAlpha(51);
TRANSPARENT Alanı
Şöyle yaparız.


int color = Color.TRANSPARENT;

Parcelable Arayüzü

Giriş
Şu satırı dahil ederiz.
import android.os.Parcelable;
Açıklaması şöyle.
1. Parcelable is an Android specific interface where you implement the serialization yourself. It was created to be far more efficient that Serializable, and to get around some problems with the default Java serialization scheme.
2. Serializable is a standard Java interface. You simply mark a class Serializable by implementing the interface, and Java will automatically serialize it in certain situations.
Bir başka açıklama şöyle
Parcelable is used for complex models.
Parcelable aslında serializable gibi düşünülmeli.

IDE'ler model sınıflarımız için Parcelable Code Generator plugin'leri sunuyor. Elle kodlamak yerine bunları kullanmak daha iyi.

Tanımlama
Şöyle yaparız.
class Car implements Parcelable {
  public int regId;
  public String brand;
  public String color;

private Car(Parcel source) {
  regId = source.readInt();
  brand = source.readString();
  color = source.readString();
}

public Car(int regId, String brand, String color) { 
  this.regId = regId;
  this.brand = brand;
  this.color = color;
}

public int describeContents() {
  return this.hashCode();
}

public void writeToParcel(Parcel dest, int flags) {
  dest.writeInt(regId);
  dest.writeString(brand);
  dest.writeString(color);
}

public static final Parcelable.Creator CREATOR
         = new Parcelable.Creator() {
  public Car createFromParcel(Parcel in) {
    return new Car(in);
  }

  public Car[] newArray(int size) {
    return new Car[size];
  }
};
}

CREATOR Alanı
Sınıfımızı Parcel'den yaratabilmek için gerekir. Şöyle yaparız.
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
  public Student createFromParcel(Parcel in) {
    return new Student(in); 
  }

  public Student[] newArray(int size) {
    return new Student[size];
  }
};
describeContents metodu
Kalıtan sınıf tarafından override edilir. Kalıtan sınıfta şöyle yaparız.
@Overridepublic int describeContents(){
  return 0;
}
writeToParcel metodu
Kalıtan sınıf tarafından override edilir. Serialization işlemini gerçekleştirilir. Kalıtan sınıfta şöyle yaparız.
@Override
public void writeToParcel(Parcel dest, int flags) {
  dest.writeStringArray(new String[] {this.id,
                                      this.name,
                                      this.grade});
}