Android Learning notes Advanced 19 to the picture and border

Source: Internet
Author: User

[Java]View PlainCopy
    1. Set color
    2. public void setcolour (int color) {
    3. CO = color;
    4. }
    5. //Set Border width
    6. public void setborderwidth (int width) {
    7. BorderWidth = width;
    8. }


Specific implementation:

[Java]View PlainCopy
  1. Package xiaosi.imageborder;
  2. Import android.app.Activity;
  3. Import Android.graphics.Color;
  4. Import Android.os.Bundle;
  5. Public class Imageborderactivity extends Activity {
  6. /** Called when the activity is first created. * /
  7. private Myimageview image = null;
  8. private Myimageview image1 = null;
  9. @Override
  10. public void OnCreate (Bundle savedinstancestate) {
  11. super.oncreate (savedinstancestate);
  12. Setcontentview (R.layout.main);
  13. Image = (Myimageview) Findviewbyid (R.ID.IAMGE);
  14. Image.setcolour (Color.yellow);
  15. Image.setborderwidth (10);
  16. Image1 = (Myimageview) Findviewbyid (r.id.iamge1);
  17. Image1.setcolour (Color.green);
  18. Image1.setborderwidth (5);
  19. }
  20. }


Main.xml

[Java]View PlainCopy
  1. <linearlayout
  2. xmlns:android="Http://schemas.android.com/apk/res/android"
  3. android:background="@drawable/playerbackground"
  4. Android:layout_width="Fill_parent"
  5. android:layout_height="Fill_parent" >
  6. <xiaosi.imageborder.myimageview
  7. android:id="@+id/iamge"
  8. Android:layout_width="200px"
  9. android:layout_height="230px"
  10. android:layout_alignparentright="true"
  11. android:src="@drawable/V"
  12. android:layout_centerinparent="true"
  13. android:layout_marginright="3px"
  14. />
  15. <xiaosi.imageborder.myimageview
  16. android:id="@+id/iamge1"
  17. Android:layout_width="200px"
  18. android:layout_height="230px"
  19. android:layout_alignparentright="true"
  20. android:src="@drawable/V"
  21. android:layout_centerinparent="true"
  22. android:layout_marginright="3px"
  23. />
  24. </LinearLayout>


[Java]View PlainCopy
  1. Package xiaosi.imageborder;
  2. Import Android.content.Context;
  3. Import Android.graphics.Canvas;
  4. Import Android.graphics.Paint;
  5. Import Android.graphics.Rect;
  6. Import Android.util.AttributeSet;
  7. Import Android.widget.ImageView;
  8. Public class Myimageview extends ImageView {
  9. private int co;
  10. private int borderwidth;
  11. Public Myimageview (context context) {
  12. super (context);
  13. }
  14. Public Myimageview (context context, AttributeSet attrs,
  15. int Defstyle) {  
  16. Super (context, attrs, Defstyle);
  17. }
  18. Public Myimageview (context context, AttributeSet attrs) {
  19. Super (context, attrs);
  20. }
  21. //Set color
  22. public void setcolour (int color) {
  23. CO = color;
  24. }
  25. //Set Border width
  26. public void setborderwidth (int width) {
  27. BorderWidth = width;
  28. }
  29. @Override
  30. protected void OnDraw (canvas canvas) {
  31. Super.ondraw (canvas);
  32. //Draw Border
  33. Rect rec = canvas.getclipbounds ();
  34. rec.bottom--;
  35. rec.right--;
  36. Paint paint = new paint ();
  37. //Set Border color
  38. Paint.setcolor (CO);
  39. Paint.setstyle (Paint.Style.STROKE);
  40. //Set Border width
  41. Paint.setstrokewidth (BorderWidth);
  42. Canvas.drawrect (REC, paint);
  43. }
  44. }


Source code Download: Click to open the link

Android Learning notes Advanced 19 to the picture and border

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.