java - Android buttonimage shake animation -
I'm new to Android and looking for a way to shake my button to click on the click. I've got it yet, but it crashes all the time or if you click it, then nothing works; I hope you guys can help me if I forget anything Say it. The code can be messy.
I have found an anonymous folder. With Shakeanim.xml
code:
activity_main.xml
& lt; Relative layout xmlns: android = "http://schemas.android.com / apk / res / android" xmlns: tool = "http://schemas.android.com/tools" Android: layout_width = "match_parent" Android: layout_height Android: paddingBottom = "@ Dimen / activity_vertical_margin" Android: paddingLeft = "@ Dimen / activity_horizontal_margin" Android: paddingRight = "@ Dimen / activity_horizontal_margin" Android: paddingTop = "@ Dimen / activity_vertical_margin" tool: reference = ". MainActivity "& gt; & Lt; ImageButton android: id = "@ + id / imageButton1" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_centerHorizontal = "true" Android: layout_centerVertical = "true" android: src = "@ drawable / chest" Android: background = "@ tap" /> & Lt; TextView android: id = "@ + id / textView1" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_above = "@ + id / imageButton1" Android: layout_centerHorizontal = "true" Android: layout_marginBottom = "51 DP "Android: Text =" 100 Clicks "/> & Lt; / RelativeLayout & gt;
Main activity. Java
package com.example.egghatcher; Import android App Importroid.os.Bundle; Import android.view.View; Import android.view.View.OnClickListener; Import android.view.animation.Animation; Import android.view.animation.AnimationUtils; Import android.widget.ImageButton; Import android.widget.TextView; Public class extends the main activity (ImageButton imageButton; TextView clicks Go animation shake; private Int clicks = 100; protected override void @ saved to create (bundled Instansstet) {Suprknkret (saved Instensstet); setContentView (R. Layoutkactivity_main); Shake = Animeshnushili Klodanimeshn (this R. Anime. Skenim);} public void addListenerOnButton () {imageButton = (ImageButton) findViewById (R.id.imageButton1); Klikongu = (Tekstdrishy) searching for Vivibiaiaidi ( Rkdktekstwu 1); imageButton.setOnClickListener (New OnClickListener () {@Override see public void onClick (v) {clicks--; clicksToGo.setText (+ and + "to open it more often" "you click Need "); FindViewById (R.id.imageButton1). Start Animation (Shake);}})}}
shakeanim.xml
& Lt ;? Xml version = "1.0" encoding = "UTF-8"? & Gt; Translation xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Period = " 300 "Android: fromXDelta =" 0 "Android: Interpolator =" @Android: Anime / Cycle_Interpolar "Android: Tuxed Lta = "10" / & gt;
Change your shakeanim.xml file with this.
& lt; Set xmlns: android = "http://schemas.android.com/apk/res/android" & gt; & Lt; Do Android Translation: duration = "150" Android: fromXDelta = "- 10%" Android: Number of repeat = "5" Android: Repeat mode = "reverse" Android: Toksdalta = "10%" / & gt; & Lt; / Set & gt;
btw You can change your MainActivity "setOnClickListener" in this way as you have already declared ImageButton.
imageButton.setOnClickListener (New OnClickListener) {@Override Click Public Zero (click V) {Click - ClickTugo .set Text ("You" + Click + "to open it More often click "); Image button. Start animation (shake);}});
If it helps, then mark it as the correct answer. Thanks
Comments
Post a Comment