Skip to content

Instantly share code, notes, and snippets.

@maunashjani
Created September 25, 2017 06:31
Show Gist options
  • Save maunashjani/e7b7a3de700c3ff318a77e24d952ea63 to your computer and use it in GitHub Desktop.
Save maunashjani/e7b7a3de700c3ff318a77e24d952ea63 to your computer and use it in GitHub Desktop.
package example.graphicsanimationdemo1;
import android.graphics.Color;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.TransitionDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.os.Bundle;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DrawableGraphic();
ShapeDrawableGraphic();
}
public void DrawableGraphic() {
//Get Parent Layout
RelativeLayout rl = (RelativeLayout) findViewById(R.id.mainLayout);
//Create a dynamic ImageView object to place it within the Relative Layout
ImageView demoimg = new ImageView(MainActivity.this);
//Get the Image Source from @drawable, here demo.png
demoimg.setImageResource(R.drawable.demo);
//Specify the placement of ImageView within the RelativeLayout
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(300, 300);
//Add rule to align the image to the left of the parent.
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
demoimg.setLayoutParams(params);
//Add ImageView within the Relative Layout
rl.addView(demoimg);
}
public void ShapeDrawableGraphic() {
//Assign Shape Properties
int alpha = 127;
int width = 300;
int height = 300;
int padding = 10;
//Get Parent Layout
RelativeLayout rl = (RelativeLayout)findViewById(R.id.mainLayout);
// Create Shape 2
ShapeDrawable shape2 = new ShapeDrawable(new RectShape());
shape2.getPaint().setColor(Color.CYAN);
shape2.setIntrinsicHeight(height);
shape2.setIntrinsicWidth(width);
shape2.setAlpha(alpha);
// Put Shape 2 into an ImageView
ImageView img = new ImageView(getApplicationContext());
img.setImageDrawable(shape2);
img.setPadding(padding, padding, padding, padding);
//Specify the placement of ImageView within the RelativeLayout
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(height, width);
//Add rule to align the image to the left of the parent.
params.addRule(RelativeLayout.CENTER_IN_PARENT);
img.setLayoutParams(params);
//Add ImageView within the Relative Layout
rl.addView(img);
}
public void Animation(View v){
//Get the Shape Transition Drawable Objects
TransitionDrawable transition = (TransitionDrawable)
ResourcesCompat.getDrawable(getResources(), R.drawable.shape_transition, null);
//Apply Effect
transition.setCrossFadeEnabled(true);
//Assign the effect to an ImageView object
((ImageView) findViewById (R.id.imgShape2)).setImageDrawable(transition);
//Set the Transition Effect Time
transition.startTransition(2000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment