Pastebin is 300% more awesome when you are logged in. Sign Up, it's FREE!
Guest

Untitled

By: a guest on Dec 11th, 2012  |  syntax: None  |  size: 2.09 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package com.example.customview;
  2.  
  3. import android.content.Context;
  4. import android.graphics.Canvas;
  5. import android.graphics.Color;
  6. import android.graphics.Paint;
  7. import android.graphics.Rect;
  8. import android.util.AttributeSet;
  9. import android.view.Display;
  10. import android.view.View;
  11. import android.view.WindowManager;
  12.  
  13. public class CustomRect extends View {
  14.  
  15.     Rect rect;
  16.     Paint blue;
  17.  
  18.     public void init() {
  19.  
  20.         rect = new Rect(0, 0, 200, 200);
  21.         blue = new Paint();
  22.         blue.setColor(Color.BLUE);
  23.         blue.setStyle(Paint.Style.FILL);
  24.     }
  25.  
  26.     public CustomRect(Context context, AttributeSet attrs, int defStyle) {
  27.         super(context, attrs, defStyle);
  28.  
  29.         init();
  30.     }
  31.  
  32.     public CustomRect(Context context, AttributeSet attrs) {
  33.         super(context, attrs);
  34.  
  35.         init();
  36.     }
  37.  
  38.     public CustomRect(Context context) {
  39.         super(context);
  40.  
  41.         init();
  42.     }
  43.  
  44.     @Override
  45.     protected void onDraw(Canvas canvas) {
  46.  
  47.         super.onDraw(canvas);
  48.  
  49.         canvas.drawRect(rect, blue);
  50.  
  51.         invalidate();
  52.     }
  53.  
  54.     @Override
  55.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  56.         // TODO Auto-generated method stub
  57.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  58.  
  59.         setMeasuredDimension(200, 200);
  60.     }  
  61. }
  62.        
  63. package com.example.customview;
  64. import android.app.Activity;
  65. import android.os.Bundle;
  66.  
  67. public class RectActivity extends Activity{
  68.  
  69.     CustomRect rect;
  70.  
  71.     @Override
  72.     protected void onCreate(Bundle savedInstanceState) {
  73.         // TODO Auto-generated method stub
  74.         super.onCreate(savedInstanceState);
  75.  
  76.         rect = new CustomRect(this);
  77.         setContentView(rect);
  78.     }
  79. }
  80.        
  81. <?xml version="1.0" encoding="utf-8"?>
  82. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  83.     android:layout_width="fill_parent"
  84.     android:layout_height="fill_parent"
  85.     android:orientation="vertical" >
  86.  
  87.     <view
  88.         android:id="@+id/thisId"
  89.         android:layout_width="wrap_content"
  90.         android:layout_height="wrap_content"
  91.         class="com.example.customview.CustomRect" />
  92.  
  93. </LinearLayout>