1 / 34

Android View Stuff

Android View Stuff. TextViews. Display text Display images???. TextView Drawables. TextViews allow drawables to appear to the left of, above, to the right of, and below the text. <? xml version = "1.0" encoding = "utf-8" ?>

eve
Download Presentation

Android View Stuff

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Android View Stuff

  2. TextViews • Display text • Display images???

  3. TextViewDrawables • TextViewsallow drawables to appear to the left of, above, to the right of, and below the text.

  4. <?xml version="1.0" encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextViewandroid:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableLeft="@drawable/ic_launcher" android:text="Drawable Left" android:layout_margin="10dp"/> <TextViewandroid:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableTop="@drawable/ic_launcher" android:text="Drawable Top" android:layout_margin="10dp"/> <TextViewandroid:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableRight="@drawable/ic_launcher" android:text="Drawable Right" android:layout_margin="10dp"/> <TextViewandroid:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableBottom="@drawable/ic_launcher" android:text="Drawable Bottom" android:layout_margin="10dp"/> </LinearLayout>

  5. ImageView • Use this View when you want to display an image in your application. • Many beginners will misuse the ImageView by using the incorrect property.

  6. ImageViewsrc property • Use the android:src property to set a drawable as the content of the ImageView. • Don’t use android:background unless you want the image to have a background img.

  7. ImageView Example <ImageViewandroid:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher"/>

  8. ImageView Example with a background <ImageViewandroid:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" android:background="#FFFF0000"/>

  9. ImageViewscaleType • Control how your image is scaled and positioned inside the ImageView • Useful when your image is too big • Useful when your image is too small

  10. ImageViewscaleType : center

  11. ImageViewscaleType : centerCrop

  12. ImageViewscaleType : centerInside

  13. ImageViewscaleType : fitCenter

  14. ImageViewscaleType : fitStart

  15. ImageViewscaleType : fitEnd

  16. ImageViewscaleType : fitXY

  17. ImageViewscaleType : matrix

  18. ImageViewScaleType Info • http://www.peachpit.com/articles/article.aspx?p=1846580&seqNum=2

  19. ImageButton • We’re familiar with a Button • Default background provided by the platform • Displays Text • Well Android supports an ImageButton • Looks like a regular button • Default background provided by the platform • Displays an Image

  20. Why Buttons are awesome • Android provides a method for giving a button a state list that defines which images should be shown while the button is: • Normal • Focused • Enabled • Disabled • Pressed

  21. Hiding the default background on an ImageButton • If you want to use an ImageButton but don’t want to see the default background, you can hide it. • Set the android:background=“#00000000” • Set the android:background=“@android:color/transparent

  22. ShapeDrawables • An XML file that defines a geometric shape, including colors and gradients. Creates a ShapeDrawable.

  23. <?xml version="1.0" encoding="utf-8"?><shapexmlns:android="http://schemas.android.com/apk/res/android"android:shape=["rectangle" | "oval" | "line" | "ring"] ><cornersandroid:radius="integer"android:topLeftRadius="integer"android:topRightRadius="integer"android:bottomLeftRadius="integer"android:bottomRightRadius="integer"/><gradientandroid:angle="integer"android:centerX="integer"android:centerY="integer"android:centerColor="integer"android:endColor="color"android:gradientRadius="integer"android:startColor="color"android:type=["linear" | "radial" | "sweep"]android:useLevel=["true" | "false"] /><paddingandroid:left="integer"android:top="integer"android:right="integer"android:bottom="integer"/><sizeandroid:width="integer"android:height="integer"/><solidandroid:color="color"/><strokeandroid:width="integer"android:color="color"android:dashWidth="integer"android:dashGap="integer"/></shape>

  24. ShapeDrawable Examples

  25. ShapeDrawable Examples

  26. OnTouchListener Interface definition for a callback to be invoked when a touch event is dispatched to this view. The callback will be invoked before the touch event is given to the view.

  27. Using OnTouchListener • Creating an OnTouchListener is exactly like creating an OnClick Listener. • The View provides a setOnTouchListener() • You can create the listener using any of the 3 methods • Anonymous Inner Class Listener • Create an OnTouchListener object • Make the class implement the OnTouchListener interface.

  28. onTouch callback

  29. MotionEvent Object • Object used to report movement (mouse, pen, finger, trackball) events. • Motion events may hold either absolute or relative movements and other data, depending on the type of device.

  30. Important parts of the MotionEvent • Action - What kind of action is being performed with the MotionEvent? • X - The x coordinate for this event • Y - The y coordinate for this event

  31. MotionEvent’s action • ACTION_DOWN - A pressed gesture has started, the motion contains the initial starting location. • ACTION_MOVE - A change has happened during a press gesture (between ACTION_DOWN and ACTION_UP). • ACTION_UP - A pressed gesture has finished, the motion contains the final release location as well as any intermediate points since the last down or move event. • ACTION_CANCEL - The current gesture has been aborted.

  32. MotionEvent’s X Coordinate • getX() - Returns the X coordinate of this event for the first index. • This coordinate is relative to the View • getRawX() - Returns the original raw X coordinate of this event. • For touch events on the screen, this is the original location of the event on the screen, before it had been adjusted for the containing window and views.

  33. MotionEvent’s Y Coordinate • getY() - Returns the Y coordinate of this event for the first index. • This coordinate is relative to the View • getRawY() - Returns the original raw Y coordinate of this event. • For touch events on the screen, this is the original location of the event on the screen, before it had been adjusted for the containing window and views.

  34. How to handle a touch event @Override publicbooleanonTouch(View v, MotionEvent event) { int action = event.getAction(); finalintx = (int) event.getX(); finalinty = (int) event.getY(); switch (action) { caseMotionEvent.ACTION_DOWN : //notify the view a down event has happened. This is the //mechanism that triggers the beginning of a click and long press. break; caseMotionEvent.ACTION_MOVE : //notify the view a move event has happened. This could //be used to scroll a view. break; caseMotionEvent.ACTION_UP : caseMotionEvent.ACTION_CANCEL : //notify the view the touch event has ended //usually you'll process the up and the cancel as if they //were the same event. break; } //return true if the view consumed the event and false if not //so one of this view's parent can receive the touch event and //process. returntrue; }

More Related