Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vector drawables #2

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
<manifest package="fmuntenescu.playground"
xmlns:android="http://schemas.android.com/apk/res/android">

<!--Uncomment the read and write storage rights when using method tracing-->
<!--<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>-->
<!--<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>-->

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep it simply, make your min sdk 4.4. then you dont need this anymore as long as you stick to getExternalFilesDir())
https://developer.android.com/guide/topics/manifest/uses-permission-element.html

<application
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
Expand All @@ -18,6 +22,9 @@
<activity
android:name=".animations.AnimationPlaygroundActivity"
android:label="@string/animations_playground" />
<activity
android:name=".vectordrawables.VectorDrawablesPlaygroundActivity"
android:label="@string/vector_drawables_playground" />
</application>

</manifest>
15 changes: 12 additions & 3 deletions app/src/main/java/fmuntenescu/playground/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.view.View;

import fmuntenescu.playground.animations.AnimationPlaygroundActivity;
import fmuntenescu.playground.vectordrawables.VectorDrawablesPlaygroundActivity;

public class MainActivity extends AppCompatActivity {

Expand All @@ -15,10 +16,18 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

View view = findViewById(R.id.animations_playground_button);
View startAnimationsPlayground = findViewById(R.id.animations_playground_button);

assert view != null;
view.setOnClickListener(v -> startActivity(AnimationPlaygroundActivity.class));
assert startAnimationsPlayground != null;
startAnimationsPlayground
.setOnClickListener(v -> startActivity(AnimationPlaygroundActivity.class));

View startVectorDrawablesPlayground = findViewById(
R.id.vector_drawables_playground_button);

assert startVectorDrawablesPlayground != null;
startVectorDrawablesPlayground
.setOnClickListener(v -> startActivity(VectorDrawablesPlaygroundActivity.class));
}

private void startActivity(@NonNull final Class<?> cls) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package fmuntenescu.playground.vectordrawables;

import android.content.Context;
import android.graphics.Canvas;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;

/**
* ImageView that measures the time took to draw.
*/
public class MeasurableImageView extends ImageView {

@Nullable
private ViewRedrawnListener mViewRedrawnListener;

public MeasurableImageView(final Context context) {
super(context);
}

public MeasurableImageView(final Context context, final AttributeSet attrs) {
super(context, attrs);
}

public MeasurableImageView(final Context context, final AttributeSet attrs,
final int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

public void setViewRedrawnListener(@NonNull final ViewRedrawnListener viewRedrawnListener) {
assert viewRedrawnListener != null;

mViewRedrawnListener = viewRedrawnListener;
}

@Override
protected void onDraw(final Canvas canvas) {
long startTime = System.currentTimeMillis();

super.onDraw(canvas);

long endTime = System.currentTimeMillis();
notifyDraw(startTime, endTime);
}

private void notifyDraw(final long startTime, final long endTime) {
long duration = endTime - startTime;

Log.d("playground", "rendering took " + duration);

if (mViewRedrawnListener != null) {
mViewRedrawnListener.onRedraw(duration);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package fmuntenescu.playground.vectordrawables;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.RadioGroup;
import android.widget.TextView;

import fmuntenescu.playground.R;

/**
* Playground for Vector Drawables.
*/
public class VectorDrawablesPlaygroundActivity extends AppCompatActivity {

@Nullable
private TextView mDurationText;

@Nullable
private MeasurableImageView mMeasurableImageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.vector_drawables_playground_activity);

mDurationText = (TextView) findViewById(R.id.draw_duration_text);
mMeasurableImageView = (MeasurableImageView) findViewById(
R.id.measurable_image);

assert mMeasurableImageView != null;
mMeasurableImageView.setViewRedrawnListener(this::updateDuration);

RadioGroup group = (RadioGroup) findViewById(R.id.drawable_choice);
assert group != null;
group.setOnCheckedChangeListener((group1, checkedId) -> onImageTypeChanged(checkedId));
group.check(R.id.vector_drawable_button);
}

private void updateDuration(final long miliseconds) {
assert mDurationText != null;

mDurationText.setText(getString(R.string.duration, miliseconds));
}

private void onImageTypeChanged(@IdRes final int checkedId) {
switch (checkedId) {
case R.id.vector_drawable_button:
vectorDrawableSelected();
break;
case R.id.png_button:
pngSelected();
break;
}
}

private void vectorDrawableSelected() {
assert mMeasurableImageView != null;
mMeasurableImageView.setImageResource(R.drawable.placeholder);
}

private void pngSelected() {
assert mMeasurableImageView != null;
mMeasurableImageView.setImageResource(R.drawable.placeholder_png);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package fmuntenescu.playground.vectordrawables;

/**
* Listener that notifies when a view has been redrawn
*/
public interface ViewRedrawnListener {

/**
* Called when the view has been redrawn
*
* @param miliseconds the time took to draw the view.
*/
void onRedraw(long miliseconds);
}
50 changes: 50 additions & 0 deletions app/src/main/res/drawable/placeholder.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="336dp"
android:height="548dp"
android:viewportHeight="548"
android:viewportWidth="336">

<group
android:translateX="-12.000000"
android:translateY="-36.000000">
<group
android:translateX="12.000000"
android:translateY="36.000000">
<path
android:name="Mask"
android:fillColor="#FFFFFF"
android:pathData="M 4 0 L 332 0 Q 336 0 336 4 L 336 544 Q 336 548 332 548 L 4 548 Q 0 548 0 544 L 0 4 Q 0 0 4 0 Z" />
<path
android:name="Rectangle-1709"
android:fillColor="#DF6B9ED1"
android:pathData="M 16 280 H 312 V 292 H 16 V 280 Z" />
<path
android:name="Rectangle-1709"
android:fillColor="#DF6B9ED1"
android:pathData="M 16 304 H 267 V 316 H 16 V 304 Z" />
<path
android:name="Rectangle-1709"
android:fillColor="#DF6B9ED1"
android:pathData="M 16 328 H 307 V 340 H 16 V 328 Z" />
<path
android:name="Rectangle-1709"
android:fillColor="#DF6B9ED1"
android:pathData="M 16 354 H 213 V 366 H 16 V 354 Z" />
<path
android:name="Rectangle-1709"
android:fillAlpha="0.9"
android:fillColor="#DF6B9ED1"
android:pathData="M 16 207 H 316 V 227 H 16 V 207 Z"
android:strokeAlpha="0.9" />
<path
android:name="Rectangle-1709"
android:fillColor="#DF6B9ED1"
android:pathData="M 16 240 H 284 V 260 H 16 V 240 Z" />
<path
android:name="Mask"
android:fillColor="#DF6B9ED1"
android:pathData="M 4 0 L 332 0 Q 336 0 336 4 L 336 188 Q 336 192 332 192 L 4 192 Q 0 192 0 188 L 0 4 Q 0 0 4 0 Z" />
</group>
</group>
</vector>
Binary file added app/src/main/res/drawable/placeholder_png.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 7 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:weightSum="3"
tools:context="fmuntenescu.playground.MainActivity">

<Button
Expand All @@ -17,4 +16,11 @@
android:layout_height="wrap_content"
android:text="@string/animations_playground" />

<Button
android:id="@+id/vector_drawables_playground_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:text="@string/vector_drawables_playground" />

</LinearLayout>
50 changes: 50 additions & 0 deletions app/src/main/res/layout/vector_drawables_playground_activity.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<fmuntenescu.playground.vectordrawables.MeasurableImageView
android:id="@+id/measurable_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/draw_duration_text" />

<RadioGroup
android:id="@+id/drawable_choice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:textSize="@dimen/text_size_normal">

<RadioButton
android:id="@+id/vector_drawable_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/vector_drawable"
android:textSize="@dimen/text_size_normal" />

<RadioButton
android:id="@+id/png_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:text="@string/png"
android:textSize="@dimen/text_size_normal" />
</RadioGroup>

<TextView
android:id="@+id/draw_duration_text"
android:layout_width="match_parent"
android:layout_height="@dimen/animating_view_height"
android:layout_alignParentBottom="true"
android:background="@color/colorPrimary"
android:gravity="center_vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:textColor="@android:color/white"
android:textSize="@dimen/text_size_normal" />
</RelativeLayout>
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@
<string name="tap_above">Tapped on the view above</string>
<string name="tap_below">Tapped on the view below</string>
<string name="tap_none">Tapped outside the above/below views</string>

<string name="vector_drawables_playground">Vector Drawables Playground</string>
<string name="vector_drawable">Vector Drawable</string>
<string name="png">PNG</string>
<string name="duration">Rendering duration: %s</string>
</resources>