android view stuff. textviews display text display images???

34
Android View Stuff

Upload: berenice-morton

Post on 21-Jan-2016

228 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Android View Stuff. TextViews Display text Display images???

Android View Stuff

Page 2: Android View Stuff. TextViews Display text Display images???

TextViews

• Display text• Display images???

Page 3: 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.

Page 4: Android View Stuff. TextViews Display text Display images???

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableLeft="@drawable/ic_launcher" android:text="Drawable Left" android:layout_margin="10dp"/>

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableTop="@drawable/ic_launcher" android:text="Drawable Top" android:layout_margin="10dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableRight="@drawable/ic_launcher" android:text="Drawable Right" android:layout_margin="10dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableBottom="@drawable/ic_launcher" android:text="Drawable Bottom" android:layout_margin="10dp"/></LinearLayout>

Page 5: Android View Stuff. TextViews Display text Display images???

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.

Page 6: Android View Stuff. TextViews Display text Display images???

ImageView src 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.

Page 7: Android View Stuff. TextViews Display text Display images???

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

Page 8: Android View Stuff. TextViews Display text Display images???

ImageView Example with a background

<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" android:background="#FFFF0000"/>

Page 9: Android View Stuff. TextViews Display text Display images???

ImageView scaleType

• 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

Page 10: Android View Stuff. TextViews Display text Display images???

ImageView scaleType : centerScale Type Description

center Displays the image centered in the view with no scaling.

centerCrop Scales the image such that both the x and y dimensions are greater than or equal to the view, while maintaining the image aspect ratio; crops any part of the image that exceeds the size of the view; centers the image in the view.

centerInside Scales the image to fit inside the view, while maintaining the image aspect ratio. If the image is already smaller than the view, then this is the same as center.

fitCenter Scales the image to fit inside the view, while maintaining the image aspect ratio. At least one axis will exactly match the view, and the result is centered inside the view.

fitStart Same as fitCenter but aligned to the top left of the view.

fitEnd Same as fitCenter but aligned to the bottom right of the view.

fitXY Scales the x and y dimensions to exactly match the view size; does not maintain the image aspect ratio.

matrix Scales the image using a supplied Matrix class. The matrix can be supplied using the setImageMatrix method. A Matrix class can be used to apply transformations such as rotations to an image.

Page 11: Android View Stuff. TextViews Display text Display images???

ImageView scaleType : centerCropScale Type Description

center Displays the image centered in the view with no scaling.

centerCrop Scales the image such that both the x and y dimensions are greater than or equal to the view, while maintaining the image aspect ratio; crops any part of the image that exceeds the size of the view; centers the image in the view.

centerInside Scales the image to fit inside the view, while maintaining the image aspect ratio. If the image is already smaller than the view, then this is the same as center.

fitCenter Scales the image to fit inside the view, while maintaining the image aspect ratio. At least one axis will exactly match the view, and the result is centered inside the view.

fitStart Same as fitCenter but aligned to the top left of the view.

fitEnd Same as fitCenter but aligned to the bottom right of the view.

fitXY Scales the x and y dimensions to exactly match the view size; does not maintain the image aspect ratio.

matrix Scales the image using a supplied Matrix class. The matrix can be supplied using the setImageMatrix method. A Matrix class can be used to apply transformations such as rotations to an image.

Page 12: Android View Stuff. TextViews Display text Display images???

ImageView scaleType : centerInsideScale Type Description

center Displays the image centered in the view with no scaling.

centerCrop Scales the image such that both the x and y dimensions are greater than or equal to the view, while maintaining the image aspect ratio; crops any part of the image that exceeds the size of the view; centers the image in the view.

centerInside Scales the image to fit inside the view, while maintaining the image aspect ratio. If the image is already smaller than the view, then this is the same as center.

fitCenter Scales the image to fit inside the view, while maintaining the image aspect ratio. At least one axis will exactly match the view, and the result is centered inside the view.

fitStart Same as fitCenter but aligned to the top left of the view.

fitEnd Same as fitCenter but aligned to the bottom right of the view.

fitXY Scales the x and y dimensions to exactly match the view size; does not maintain the image aspect ratio.

matrix Scales the image using a supplied Matrix class. The matrix can be supplied using the setImageMatrix method. A Matrix class can be used to apply transformations such as rotations to an image.

Page 13: Android View Stuff. TextViews Display text Display images???

ImageView scaleType : fitCenterScale Type Description

center Displays the image centered in the view with no scaling.

centerCrop Scales the image such that both the x and y dimensions are greater than or equal to the view, while maintaining the image aspect ratio; crops any part of the image that exceeds the size of the view; centers the image in the view.

centerInside Scales the image to fit inside the view, while maintaining the image aspect ratio. If the image is already smaller than the view, then this is the same as center.

fitCenter Scales the image to fit inside the view, while maintaining the image aspect ratio. At least one axis will exactly match the view, and the result is centered inside the view.

fitStart Same as fitCenter but aligned to the top left of the view.

fitEnd Same as fitCenter but aligned to the bottom right of the view.

fitXY Scales the x and y dimensions to exactly match the view size; does not maintain the image aspect ratio.

matrix Scales the image using a supplied Matrix class. The matrix can be supplied using the setImageMatrix method. A Matrix class can be used to apply transformations such as rotations to an image.

Page 14: Android View Stuff. TextViews Display text Display images???

ImageView scaleType : fitStartScale Type Description

center Displays the image centered in the view with no scaling.

centerCrop Scales the image such that both the x and y dimensions are greater than or equal to the view, while maintaining the image aspect ratio; crops any part of the image that exceeds the size of the view; centers the image in the view.

centerInside Scales the image to fit inside the view, while maintaining the image aspect ratio. If the image is already smaller than the view, then this is the same as center.

fitCenter Scales the image to fit inside the view, while maintaining the image aspect ratio. At least one axis will exactly match the view, and the result is centered inside the view.

fitStart Same as fitCenter but aligned to the top left of the view.

fitEnd Same as fitCenter but aligned to the bottom right of the view.

fitXY Scales the x and y dimensions to exactly match the view size; does not maintain the image aspect ratio.

matrix Scales the image using a supplied Matrix class. The matrix can be supplied using the setImageMatrix method. A Matrix class can be used to apply transformations such as rotations to an image.

Page 15: Android View Stuff. TextViews Display text Display images???

ImageView scaleType : fitEndScale Type Description

center Displays the image centered in the view with no scaling.

centerCrop Scales the image such that both the x and y dimensions are greater than or equal to the view, while maintaining the image aspect ratio; crops any part of the image that exceeds the size of the view; centers the image in the view.

centerInside Scales the image to fit inside the view, while maintaining the image aspect ratio. If the image is already smaller than the view, then this is the same as center.

fitCenter Scales the image to fit inside the view, while maintaining the image aspect ratio. At least one axis will exactly match the view, and the result is centered inside the view.

fitStart Same as fitCenter but aligned to the top left of the view.

fitEnd Same as fitCenter but aligned to the bottom right of the view.

fitXY Scales the x and y dimensions to exactly match the view size; does not maintain the image aspect ratio.

matrix Scales the image using a supplied Matrix class. The matrix can be supplied using the setImageMatrix method. A Matrix class can be used to apply transformations such as rotations to an image.

Page 16: Android View Stuff. TextViews Display text Display images???

ImageView scaleType : fitXYScale Type Description

center Displays the image centered in the view with no scaling.

centerCrop Scales the image such that both the x and y dimensions are greater than or equal to the view, while maintaining the image aspect ratio; crops any part of the image that exceeds the size of the view; centers the image in the view.

centerInside Scales the image to fit inside the view, while maintaining the image aspect ratio. If the image is already smaller than the view, then this is the same as center.

fitCenter Scales the image to fit inside the view, while maintaining the image aspect ratio. At least one axis will exactly match the view, and the result is centered inside the view.

fitStart Same as fitCenter but aligned to the top left of the view.

fitEnd Same as fitCenter but aligned to the bottom right of the view.

fitXY Scales the x and y dimensions to exactly match the view size; does not maintain the image aspect ratio.

matrix Scales the image using a supplied Matrix class. The matrix can be supplied using the setImageMatrix method. A Matrix class can be used to apply transformations such as rotations to an image.

Page 17: Android View Stuff. TextViews Display text Display images???

ImageView scaleType : matrixScale Type Description

center Displays the image centered in the view with no scaling.

centerCrop Scales the image such that both the x and y dimensions are greater than or equal to the view, while maintaining the image aspect ratio; crops any part of the image that exceeds the size of the view; centers the image in the view.

centerInside Scales the image to fit inside the view, while maintaining the image aspect ratio. If the image is already smaller than the view, then this is the same as center.

fitCenter Scales the image to fit inside the view, while maintaining the image aspect ratio. At least one axis will exactly match the view, and the result is centered inside the view.

fitStart Same as fitCenter but aligned to the top left of the view.

fitEnd Same as fitCenter but aligned to the bottom right of the view.

fitXY Scales the x and y dimensions to exactly match the view size; does not maintain the image aspect ratio.

matrix Scales the image using a supplied Matrix class. The matrix can be supplied using the setImageMatrix method. A Matrix class can be used to apply transformations such as rotations to an image.

Page 18: Android View Stuff. TextViews Display text Display images???

ImageView ScaleType Info

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

Page 19: Android View Stuff. TextViews Display text Display images???

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

Page 20: Android View Stuff. TextViews Display text Display images???

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

Page 21: Android View Stuff. TextViews Display text Display images???

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

Page 22: Android View Stuff. TextViews Display text Display images???

ShapeDrawables

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

Page 23: Android View Stuff. TextViews Display text Display images???

<?xml version="1.0" encoding="utf-8"?><shape    xmlns:android="http://schemas.android.com/apk/res/android"    android:shape=["rectangle" | "oval" | "line" | "ring"] >    <corners        android:radius="integer"        android:topLeftRadius="integer"        android:topRightRadius="integer"        android:bottomLeftRadius="integer"        android:bottomRightRadius="integer" />    <gradient        android: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"] />    <padding        android:left="integer"        android:top="integer"        android:right="integer"        android:bottom="integer" />    <size        android:width="integer"        android:height="integer" />    <solid        android:color="color" />    <stroke        android:width="integer"        android:color="color"        android:dashWidth="integer"        android:dashGap="integer" /></shape>

Page 24: Android View Stuff. TextViews Display text Display images???

ShapeDrawable Examples

Page 25: Android View Stuff. TextViews Display text Display images???

ShapeDrawable Examples

Page 26: Android View Stuff. TextViews Display text Display images???

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.

Page 27: Android View Stuff. TextViews Display text Display images???

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 methods1. Anonymous Inner Class Listener2. Create an OnTouchListener object3. Make the class implement the OnTouchListener

interface.

Page 28: Android View Stuff. TextViews Display text Display images???

onTouch callback

Page 29: Android View Stuff. TextViews Display text Display images???

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.

Page 30: Android View Stuff. TextViews Display text Display images???

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

Page 31: Android View Stuff. TextViews Display text Display images???

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.

Page 32: Android View Stuff. TextViews Display text Display images???

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.

Page 33: Android View Stuff. TextViews Display text Display images???

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.

Page 34: Android View Stuff. TextViews Display text Display images???

How to handle a touch event@Overridepublic boolean onTouch(View v, MotionEvent event) {

int action = event.getAction();final int x = (int) event.getX();final int y = (int) event.getY();switch (action) {

case MotionEvent.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;

case MotionEvent.ACTION_MOVE ://notify the view a move event has happened. This could//be used to scroll a view.break;

case MotionEvent.ACTION_UP :case MotionEvent.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.return true;

}