Load Color from resource xml file : Color « 2D Graphics « Android






Load Color from resource xml file

  


package app.Test;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.os.Bundle;
import android.view.View;

public class appTest extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(new GraphicsView(this));
  }
  static public class GraphicsView extends View {
    private static final String QUOTE = "This is a test. This is a demo.";
    private Path circle;
    private Paint cPaint;
    private Paint tPaint;

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

      int color = getResources().getColor(R.color.mycolor);

      circle = new Path();
      circle.addCircle(150, 150, 100, Direction.CW);

      cPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
      cPaint.setStyle(Paint.Style.STROKE);
      cPaint.setColor(Color.LTGRAY);
      cPaint.setStrokeWidth(3);

      setBackgroundResource(R.drawable.icon);
      
      tPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
      tPaint.setStyle(Paint.Style.FILL_AND_STROKE);
      tPaint.setColor(Color.BLACK);
      tPaint.setTextSize(20f);
      setBackgroundColor(color);
    }

    @Override
    protected void onDraw(Canvas canvas) {
      canvas.drawTextOnPath(QUOTE, circle, 0, 20, tPaint);
    }
  }
}


//main.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"
   android:background="@drawable/icon">
   <org.example.graphics.Graphics.GraphicsView
      android:id="@+id/graphics"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" />
</LinearLayout>


//strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Graphics</string>

</resources>


//colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <color name="mycolor">#7fff00ff</color> 
</resources>

   
    
  








Related examples in the same category

1.Using solid color to paint
2.Set color for Paint
3.Create Color from RGB value
4.Using View to display color
5.Change check box color
6.Color Filters
7.Color Matrix Sample
8.HSV To Color
9.RGB To Color
10.lighten Color
11.Get Random Location and Colors
12.Returns the complimentary (opposite) color.
13.brighter a color
14.darker a color
15.extends View to create ColorCircle
16.Increase Red
17.Increase Contrast, Reduce Brightness
18.Decrease Saturation