Thursday, October 15, 2015

Interactive flip ImageView using ObjectAnimator

User touch on buttons to flip the ImageView forward/backward alternatively, around X-axis and Y-axis.


package com.example.androidflipview;

import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

Button buttonFlipX, buttonFlipY;
ImageView imageView;
boolean dirX = true;
boolean dirY = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView)findViewById(R.id.image);
buttonFlipX = (Button)findViewById(R.id.buttonflipX);
buttonFlipY = (Button)findViewById(R.id.buttonflipY);

buttonFlipX.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if(dirX){
dirX = false;
buttonFlipX.setText("Flip X Backward");
ObjectAnimator flip = ObjectAnimator.ofFloat(imageView, "rotationX", 0f, 180f);
flip.setDuration(500);
flip.start();
}else{
dirX = true;
buttonFlipX.setText("Flip X Forward");
ObjectAnimator flip = ObjectAnimator.ofFloat(imageView, "rotationX", 180f, 0f);
flip.setDuration(1000);
flip.start();
}
}
});

buttonFlipY.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if(dirY){
dirY = false;
buttonFlipY.setText("Flip Y Backward");
ObjectAnimator flip = ObjectAnimator.ofFloat(imageView, "rotationY", 0f, 180f);
flip.setDuration(2000);
flip.start();
}else{
dirY = true;
buttonFlipY.setText("Flip Y Forward");
ObjectAnimator flip = ObjectAnimator.ofFloat(imageView, "rotationY", 180f, 0f);
flip.setDuration(3000);
flip.start();
}
}
});
}
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
a
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android-coding.blogspot.com"
android:textSize="28dp"
android:textStyle="bold" />

<Button
android:id="@+id/buttonflipX"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Flip X Forward" />
<Button
android:id="@+id/buttonflipY"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Flip Y Forward" />

<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/ic_launcher" />
</LinearLayout>


Monday, September 14, 2015

Get Started AdMob for Android in Android Studio

AdMob uses the Google Mobile Ads SDK. This guide will show you how to integrate the Google Mobile Ads SDK into a brand new app and use it to display a simple banner ad. It should take about thirty minutes to complete and will give you a good sense of how the SDK functions within an app. If you're new to Google Mobile Ads, this is a great place to start before moving on to more advanced examples.

https://developers.google.com/admob/android/quick-start


Wednesday, September 9, 2015

Android Asset Studio

Android Asset Studio is a set of web-based tools for generating graphics and other assets that would eventually be in an Android application's res/ directory.



See the source on GitHub.

Friday, September 4, 2015

Google Android Developers Channel in YouTube

The home for videos, demos, tutorials, interviews, and anything else related to Android development. Android Developers channel at Youtube: https://www.youtube.com/user/androiddevelopers

Saturday, August 8, 2015

Re-using Bitmaps

In modern mobile applications, Bitmaps can account for a large amount of memory churn. Constantly loading thumbnails, user icons, and Emoji sets can provide your users with a steady stream of media, but it can also contribute to some HUGE pauses for garbage collection. 

In this video Colt McAnlis, and for temporally allocated bitmaps, there’s a handy trick that you absolutely should be using to escape these performance problems: Re-using bitmaps.

Every time you allocate a bitmap, you have to incur some overhead to allocate the objects from the heap, which is less than ideal if you’ve got a lot of bitmaps. Rather than banging on the heap for new objects each time, you can instead, reuse the memory that an existing bitmap has created, and load your image there.

The end result? Less memory churn from bitmaps.


Pre-scaling Bitmaps

For media rich applications, BITMAPS are everywhere. But these high-resolution images can cause a horde of performance problems if the size of the image in memory is larger than the size you’re displaying it on screen. As such one of the most important things you can do to alleviate memory pressure in your app, is resizing your bitmaps.

Rather than writing all your own image resizing code, Android has a set of APIs which can do all this work for you. But the trick is, knowing which one to use?

For example, inSampleSize is the fastest way to down-scale your image; But you can only make it smaller by some factor of your image. createScaledBitmap is a great API, but requires an extra memory allocation to get it done.

Thankfully, Colt McAnlis covers all these topics (and more) in this video, helping you reduce your memory footprint, and get some smaller images.

Sunday, July 5, 2015

Create custom text style

Example to create custom text style:


Modify /res/values/styles.xml to create our custom text style "LargeRedText", "InverseMediumBlueText", "GreenText", "ItalicGrayText" and "Bold50BlackText".
<resources xmlns:android="http://schemas.android.com/apk/res/android">

<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

<style name="LargeRedText" parent="@android:style/TextAppearance.Large">
<item name="android:textColor">#FF0000</item>
</style>
<style name="InverseMediumBlueText" parent="@android:style/TextAppearance.Medium.Inverse">
<item name="android:background">#0000FF</item>
</style>
<style name="GreenText" parent="@android:style/TextAppearance">
<item name="android:textColor">#00FF00</item>
</style>
<style name="ItalicGrayText" parent="@android:style/TextAppearance">
<item name="android:textColor">#A0A0A0</item>
<item name="android:textStyle">italic</item>
</style>
<style name="Bold50BlackText">
<item name="android:textColor">#000000</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">50dp</item>
</style>

</resources>


Example to use our custom text style in layout xml.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.androidtextappearance.MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android-coding.blogspot.com"
android:textSize="24dp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="textAppearance"
android:textAppearance="?android:textAppearance" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="textAppearanceLarge"
android:textAppearance="?android:textAppearanceLarge" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="textAppearanceMedium"
android:textAppearance="?android:textAppearanceMedium" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="textAppearanceSmall"
android:textAppearance="?android:textAppearanceSmall" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="custom style LargeRedText"
style="@style/LargeRedText" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="custom style InverseMediumBlueText"
style="@style/InverseMediumBlueText" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="custom style GreenText"
style="@style/GreenText" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="custom style ItalicGrayText"
style="@style/ItalicGrayText" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="custom style Bold50BlackText"
style="@style/Bold50BlackText" />

</LinearLayout>