gdgfresno / androidify-yourself

Androidify Yourself app for 2017 Valley DevFest, based on the GDG Riga event app
https://vdf2017-3a9f6.firebaseapp.com/participants/
MIT License
1 stars 0 forks source link

Androidify Yourself app for GDG event

screenshot

Click here for this particular app's companion conference website to see your submitted avatar

Slides

https://speakerdeck.com/larchaon/androidify-yourself

GDG Riga event link

G+ event link

Basic Functionality

<TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="20" android:text="Create your Android" />

....

*  Add Layout with Android parts selectors
``` xml
<LinearLayout
    android:layout_weight="90"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:padding="1dp"
    android:orientation="vertical"
    android:background="@drawable/border">

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPagerHead"
        android:layout_weight="30"
        android:layout_width="match_parent"
        android:layout_height="0dp" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPagerBody"
        android:layout_weight="30"
        android:layout_width="match_parent"
        android:layout_height="0dp" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPagerLegs"
        android:layout_weight="30"
        android:layout_width="match_parent"
        android:layout_height="0dp" />
</LinearLayout>

private void initShareButton(final ViewPager viewPagerHead, final ViewPager viewPagerBody, final ViewPager viewPagerLegs) { View shareButton = findViewById(R.id.button_share); shareButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { ... } }); }

* Create BitmapUtils for creating an image Bitmap
``` java
public class BitmapUtils {

    public static Bitmap getBitmap(Resources resources, int drawableResourceId) {
        return BitmapFactory.decodeResource(resources,
                drawableResourceId);
    }

    public static Bitmap combineDrawables(Resources resources, int head, int body, int legs) {
        Bitmap headBitmap = getBitmap(resources, head);
        Bitmap bodyBitmap = getBitmap(resources, body);
        Bitmap legsBitmap = getBitmap(resources, legs);

        int height = headBitmap.getHeight() + bodyBitmap.getHeight() + legsBitmap.getHeight();
        int width = Math.max(headBitmap.getWidth(), Math.max(bodyBitmap.getWidth(), legsBitmap.getWidth()));

        Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas comboImage = new Canvas(result);
        comboImage.drawBitmap(headBitmap, 0f, 0f, null);
        comboImage.drawBitmap(bodyBitmap, 0f, headBitmap.getHeight(), null);
        comboImage.drawBitmap(legsBitmap, 0f, headBitmap.getHeight() + bodyBitmap.getHeight(), null);

        return result;
    }
}

private void startShareActivity(Uri imageURI) { Intent shareIntent = new Intent(Intent.ACTION_SEND);

shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageURI);
shareIntent.setType("image/png");

startActivity(shareIntent);

}

* Oops we have an exception. Lets fix it
``` xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

}

* Initiate RecordButton in MainActivity
``` java
...
private final String mySound = "my_recorded_sound";
private AndroidSoundRecorder soundRecorder;
...

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    initRecordButton();
}

private void initRecordButton() {
    soundRecorder = new AndroidSoundRecorder(mySound);

    View recordButton = findViewById(R.id.button_record_sound);
    recordButton.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            ImageButton recordButton = (ImageButton) view;

            if (MotionEvent.ACTION_UP == motionEvent.getAction()) {
                if (soundRecorder.isRecording()) {
                    soundRecorder.stopRecording();
                    recordButton.setImageDrawable(getResources().getDrawable(R.drawable.record_off));
                } else {
                    soundRecorder.startRecording();
                    recordButton.setImageDrawable(getResources().getDrawable(R.drawable.record_on));
                }
            }

            return false;
        }
    });
}

@Override protected void onCreate(Bundle savedInstanceState) { ... initPlayButton(); }

private void initPlayButton() { soundPlayer = new AndroidSoundPlayer(mySound);

View playButton = findViewById(R.id.button_play_sound);
playButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (soundPlayer.isPlaying()) {
            soundPlayer.stopPlaying();
        } else {
            soundPlayer.startPlaying();
        }
    }
});

}

* Add permission to AndroidManifest.xml
``` xml
<uses-permission android:name="android.permission.RECORD_AUDIO"/>