This is java code.I am getting image from image gallery.I have one Button and one ImageView. It is rotating only one time.When I again click button it is not rotating image.
public class EditActivity extends ActionBarActivity
{
private Button rotate;
private ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
rotate=(Button)findViewById(R.id.btn_rotate1);
imageView = (ImageView) findViewById(R.id.selectedImage);
String path = getIntent().getExtras().getString("path");
final Bitmap bitmap = BitmapFactory.decodeFile(path);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 510, 500,
false));
rotate.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
imageView.setRotation(90);
}
});
}
Change your onClick() method to
#Override
public void onClick(View v)
{
imageView.setRotation(imageView.getRotation() + 90);
}
Notice, what the docs say
Sets the degrees that the view is rotated around the pivot point. Increasing values result in clockwise rotation.
I'd like to update my answer to show how to use RotateAnimation to achieve the same effect in case you're also targeting Android devices running Gingerbread (v10) or below.
private int mCurrRotation = 0; // takes the place of getRotation()
Introduce an instance field to track the rotation degrees as above and use it as:
mCurrRotation %= 360;
float fromRotation = mCurrRotation;
float toRotation = mCurrRotation += 90;
final RotateAnimation rotateAnim = new RotateAnimation(
fromRotation, toRotation, imageview.getWidth()/2, imageView.getHeight()/2);
rotateAnim.setDuration(1000); // Use 0 ms to rotate instantly
rotateAnim.setFillAfter(true); // Must be true or the animation will reset
imageView.startAnimation(rotateAnim);
Usually one can setup such View animations through XML as well. But, since you have to specify absolute degree values in there, successive rotations will repeat themselves instead of building upon the previous one to complete a full circle. Hence, I chose to show how to do it in code above.
Related
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
button.setOnClickListener(this);
int height = this.getWindow().getDecorView().getHeight();
int width = this.getWindow().getDecorView().getWidth();
bitmap = Bitmap.createBitmap(height, width, Bitmap.Config.ARGB_8888 );
canvas = new Canvas(bitmap);
imageView = findViewById(R.id.imageView);
}
#Override
public void onClick(View v) {
if (v.getId() == R.id.button){
canvas.drawColor(Color.RED);
}
imageView.setImageBitmap(bitmap);
}
}
Does anyone have any idea why the above code snippet for Android Studio would cause the app to crash constantly? I'm new to Android Studio and I want to get used to building rectangles and shapes before anything more, but it seems I'm having difficulty with even that.
Displays error of drawing Bitmap with values not > 0, but the issue is that after using a print function in onClick, grabbing height or width from either window or the imageView, I am returned a value > 0, so I don't understand.
You are getting height and width in onCreate method before view has been drawn and thus resulting in returning height 0, width 0 since android system don't know exact width and height of view.You can use some method like post(Runnable) to know if view has drawn.Modify you code like below.
View view = this.getWindow().getDecorView();
view.post( new Runnable() {
#Override
public void run() {
width = view.getMeasuredWidth();
height = view.getMeasuredHeight();
bitmap = Bitmap.createBitmap(height, width, Bitmap.Config.ARGB_8888 );
canvas = new Canvas(bitmap);
}
});
I am trying to set the height and width of an imageview to be twice the size of the height and width of the screen, which obviously changes depending on what device is used.
This is what I attempted (but the imageview was just invisible):
public class MainActivity extends AppCompatActivity {
private ImageView Geoline;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Geoline = findViewById(R.id.Geoline);
//Fetching the Device Resolution
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int scrWidth = size.x;
int scrHeight = size.y;
//Creating variables that are twice the size of screen resolutions
int scalex = 2 * scrWidth;
int scaley = 2 * scrHeight;
//Setting the screen width and height to equal that of variables "scalex" and "scaley"
Geoline.setScaleX(scalex);
Geoline.setScaleY(scaley);
}
You are trying to do this in onCreate() where View is not yet rendered, trying moving your code to onResume() or even better do like this
mBinding.parentView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
}
});
Move your code inside this, it will work.
i want to increase the rotation speed.How can I increase the rotation speed of the ImageView in this code ??anyone please help me.I need help with this code. Thanks in advance.
public class MainActivity extends Activity {
ImageView img_one;
Button btn_one;
Random r;
int angle;
boolean restart=false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img_one = (ImageView) findViewById(R.id.imageView_ring);
r=new Random();
btn_one = (Button) findViewById(R.id.btn_1);
btn_one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (restart){
angle=angle & 360;
angle=r.nextInt()+360;
RotateAnimation rotate=new RotateAnimation(angle,360,RotateAnimation.RELATIVE_TO_SELF,0.5f,
RotateAnimation.RELATIVE_TO_SELF,0.5f);
rotate.setFillAfter(true);
rotate.setDuration(50);
rotate.setInterpolator(new AccelerateDecelerateInterpolator());
img_one.startAnimation(rotate);
restart=false;
}else {
angle=r.nextInt()+360;
RotateAnimation rotate=new RotateAnimation(0,angle,RotateAnimation.RELATIVE_TO_SELF,0.5f,
RotateAnimation.RELATIVE_TO_SELF,0.5f);
rotate.setFillAfter(true);
rotate.setDuration(50);
rotate.setInterpolator(new AccelerateDecelerateInterpolator());
img_one.startAnimation(rotate);
restart=true;
}
}
});
}
}
If you want to rotate faster, you need to lower the duration of the animation. Currently you have it set to last for 50 milliseconds:
rotate.setDuration(50);
but you can set the duration to whatever you need to achieve your desired effect.
I want to create my custom camera with supports different color
effects for taking images ... I want to custom color effect on my
camera . i put some effect that is Built_In in device but i have no
idea that how i apply other effect like AFTER SNOW FALLING EFFECT
from here and so many other Color Effects.
Please Give me some hints or any link that is helpful for me
My code for effect is
((ImageView)findViewById(R.id.e1)).setOnClickListener(onButtonClick);
private View.OnClickListener onButtonClick = new View.OnClickListener() {
#Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.e1: MonoEffect();
break;}
}
};
private void MonoEffect()
{
Camera.Parameters parameters =mCamera.getParameters();
parameters.setColorEffect(android.hardware.Camera.Parameters.EFFECT_MONO);
mCamera.setParameters(parameters);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cameraPreviewLayout = (FrameLayout) findViewById(R.id.camera_preview);
//this code is holding an realtime overlay
RelativeLayout layout = (RelativeLayout) findViewById(R.id.preview);
FrameLayout fr = new FrameLayout(this);
fr.setBackgroundResource(R.drawable.snow);
ImageView b1 = new ImageView(this);
b1.setBackgroundResource(R.drawable.snow);
fr.addView(b1);
layout.addView(fr);
/*one solution could be,you if you want a real-time effect on your camera layout,you can add an overlay on the preview as given up.snow is a png format picture with transparent background */ after that... add both the camera output and ta transparent snoweffect as..
Bitmap cameraBitmap =BitmapFactory.decodeByteArray(data, 0, data.length);
Bitmap overlayBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.snow);
Bitmap cameraScaledBitmap = Bitmap.createScaledBitmap(cameraBitmap, 1200, 1600, true);
int wid = cameraScaledBitmap.getWidth();
int hgt = cameraScaledBitmap.getHeight();
Bitmap newImage = Bitmap.createBitmap(wid,hgt, Bitmap.Config.ARGB_8888);
Bitmap overlayScaledBitmap = Bitmap.createScaledBitmap(overlayBitmap, wid, hgt, true);
Canvas canvas = new Canvas(newImage);
canvas.drawBitmap(cameraBitmap , 0, 0, null);
canvas.drawBitmap(overlayScaledBitmap , 0, 0, null);
now you have got your final bitmap as named overlayScaledBitmap.now save it.dont tension about 1200,1600.i just scaled both the camera output bitmap and the snow transparent png as same size.i am currently working on the same project as you qustioned.i would like to take any suggetion from you. https://www.facebook.com/mohammad.mukul.37
I have two questions you might be able to help me with, I am new at programming and this might be really easy but I looked for the answers for a couple of days and have not been able to get them. I am doing a simple android application in which you enter a hexadecimal number in a plain text field and when you click on a button a canvas gets created and you see the color from that hexadecimal number in the canvas (ex:0xff000000 you would get black) unfortunately I am not able to use the string as an integer variable into the Canvas.drawcolor(), can someone tell me how can I do this correctly, and/or why what I am doing is wrong?
My second question is how can I make the canvas appear without making my button and text field disappear from the layout when it gets created?
Here is my code, I created the toast to see if I was getting the string correctly:
public class MainActivity extends Activity implements TextWatcher{
private Bitmap mBitmap;
private Canvas mCanvas;
private ImageView mImageView;
private EditText mHexadecimal;
private String comments;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mHexadecimal = (EditText) findViewById(R.id.Hexadecimal);
mHexadecimal.addTextChangedListener(this);
}
public void onClick(View v) {
mBitmap = Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
comments = mHexadecimal.getText().toString();
Toast.makeText(getApplicationContext(), comments,Toast.LENGTH_LONG).show();
mCanvas.drawColor(Integer.parseInt(comments,16));
mImageView = new ImageView(this);
mImageView.setImageBitmap(mBitmap);
setContentView(mImageView);
}
Well, you can have a try by doing such things:
String color = "#FCFC0000"; //this is your color string
int colorCode = Integer.parseInt(string.replaceFirst("^#",""), 16);
Then try to set this colorCode in your mCanvas.