Draw Dynamic Circle Ontouch Android
Your onTouch outcome check if at that place is a move. You can do it by checking if there is any movement using:
ACTION_UP
ACTION_DOWN
Practice something really unproblematic past recording the fourth dimension the user touches the screen.
private long lastTouchDown; private static int CLICK_ACTION_THRESHOLD = 200; @Override public boolean onTouch(View v, MotionEvent result) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: lastTouchDown = Organisation.currentTimeMillis(); break; example MotionEvent.ACTION_UP: if (System.currentTimeMillis() - lastTouchDown < CLICK_ACTION_THRESHOLD) { Log.due west("Test", "Y'all clicked!"); } break; } return true; }
In this postal service attached image presents both orientation(Portrait / Landscape)
To get max 10 and max Y, read on.
For Android device screen coordinates, below concept will work.
Display mDisp = getWindowManager (). getDefaultDisplay (); Signal mDispSize = new Point (); mDisp. getSize (mDispSize ); int maxX = mDispSize . x ; int maxY = mDispSize . y ;
for devices supporting android api level older than 13. Can employ beneath this lawmaking.
Display mDisp = getWindowManager (). getDefaultDisplay (); int max_X = mDisp . getWidth (); int max_Y = mDisp . getHeight ();
Let'south now focus on to Android device screen coordinates. all the co-ordinates volition be represented on px values. Android Screen as well as its view coordinates origin is (0,0) i.e. it starts from top left corner. Moving right from origin is positive Ten-axis and moving down from origin is positive Y-centrality. Take a expect upon the figure.
I am also suffering this problem afterward update Android studio two.ii to two.iii. When you are getting mistake message continuously. I disable the Instant run and run the app again App starts successfully installing in the Device without showing any error Window. I hope google will sort you these Issues with Instant run soon.
Steps to Disable Instant Run
class Android Studio :
File -> Settings -> Build,Execution,Deployment -> Instant Run -> Un-check (Enable Instant Run to hot swap code/resource changes on deploy (default enabled) )
Savour…!
CoordinatorLayout extends the power to accomplish many of the Google'southward Fabric Design.
ane. Add dependency in build.gradle file as like this.
compile 'com.android.support:pattern:23.2.0'
two. Create Layout activity_main.xml file
<?xml version="ane.0" encoding="utf-8"?> <android.support.pattern.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="truthful"> <android.support.design.widget.AppBarLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:groundwork="#e6a92f" android:theme="@way/AppTheme.AppBarOverlay"> </android.support.design.widget.AppBarLayout> <RelativeLayout android:id="@+id/main_layout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="praveen.com.test_praveen.MainActivity" > <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" > <Button android:id="@+id/button_0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/theme_0" /> <Button android:id="@+id/button_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/theme_1" android:layout_below="@+id/button_0"/> <Button android:id="@+id/button_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/theme_2" android:layout_below="@id/button_1"/> <Button android:id="@+id/button_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/theme_3" android:layout_below="@id/button_2"/> <Button android:id="@+id/button_4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/theme_4" android:layout_below="@id/button_3"/> </RelativeLayout> </RelativeLayout> </android.support.blueprint.widget.CoordinatorLayout>
3. Created MainActivity.java file
public class MainActivity extends AppCompatActivity { private ThemeUtils themeUtils; private Button theme_0; private Button theme_1; private Button theme_2; individual Push button theme_3; private Push theme_4; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); themeUtils.onActivityCreateSetTheme(this); setContentView(R.layout.activity_main); theme_0 = (Button) findViewById(R.id.button_0); theme_1 = (Button) findViewById(R.id.button_1); theme_2 = (Button) findViewById(R.id.button_2); theme_3 = (Push button) findViewById(R.id.button_3); theme_4 = (Button) findViewById(R.id.button_4); } @Override protected void onResume() { super.onResume(); try { theme_0.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { themeUtils.changeToTheme(MainActivity.this, themeUtils.DEFAULT); } }); theme_1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { themeUtils.changeToTheme(MainActivity.this, themeUtils.Yellow); } }); theme_2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { themeUtils.changeToTheme(MainActivity.this, themeUtils.Blueish); } }); theme_3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { themeUtils.changeToTheme(MainActivity.this, themeUtils.Dark-green); } }); theme_4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { themeUtils.changeToTheme(MainActivity.this, themeUtils.Scarlet); } }); } catch (Exception e) { e.printStackTrace(); } } }
4. Created ThemeUtils.java class file
public class ThemeUtils { individual static int changeTheme; public last static int Yellow = 1; public last static int BLUE = ii; public terminal static int GREEN = iii; public final static int RED = four; public final static int DEFAULT = 0; public static void changeToTheme(Activeness activeness, int theme) { changeTheme = theme; activity.terminate(); activeness.startActivity(new Intent(activity, activeness.getClass())); } public static void onActivityCreateSetTheme(Activity activity) { switch (changeTheme) { default: case DEFAULT: activeness.setTheme(R.way.AppTheme); interruption; case Xanthous: activity.setTheme(R.style.AppTheme1); break; case Blueish: activity.setTheme(R.style.AppTheme2); interruption; case GREEN: activity.setTheme(R.mode.AppTheme3); intermission; case Carmine: activeness.setTheme(R.style.AppTheme4); break; } } }
five. Created theme in styles.xml file
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <detail name="colorPrimary">@colour/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <particular proper name="colorAccent">@color/colorAccent</item> </style> <fashion proper name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" /> <fashion proper noun="AppTheme1" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorYellow</detail> <item name="colorPrimaryDark">@color/colorPrimaryDarkYellow</detail> <item name="android:windowBackground">@colour/colorYellow</item> </manner> <style proper name="AppTheme2" parent="Theme.AppCompat.Calorie-free.DarkActionBar"> <!-- Customize your theme hither. --> <item proper name="colorPrimary">@color/colorBlue</particular> <item name="colorPrimaryDark">@color/colorPrimaryDarkBlue</item> <particular name="android:windowBackground">@color/colorBlue</item> </manner> <style name="AppTheme3" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme hither. --> <detail proper name="colorPrimary">@color/colorGreen</item> <item name="colorPrimaryDark">@colour/colorPrimaryDarkGreen</item> <item proper noun="android:windowBackground">@colour/colorGreen</item> </fashion> <manner proper noun="AppTheme4" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <particular name="colorPrimary">@colour/colorRed</item> <detail name="colorPrimaryDark">@color/colorPrimaryDarkRed</item> <item name="android:windowBackground">@color/colorRed</item> </manner> </resource>
6. Added string in string.xml file
<resources> <string proper noun="app_name">Change Theme</cord> <string proper noun="theme_0">Default</string> <string name="theme_1">Yellow</string> <string name="theme_2">Bluish</string> <cord name="theme_3">Green</cord> <cord proper noun="theme_4">Red</string> </resource>
Become GAID(Google's advertising ID)
1. Download latest Google Play Services SDK.
two. Import the code and add together it as a library projection.
3. Modify AndroidManifest.xml.
<meta-data android:proper name = "com.google.android.gms.version" android:value = "@integer/google_play_services_version" />
4. Enable ProGuard to shrink and obfuscate your lawmaking in projection.properties this line.
proguard . config = $ { sdk . dir }/ tools / proguard / proguard - android . txt : proguard - project . txt
v. Add rules in proguard-projection.txt.
- keep grade * extends java . util . ListResourceBundle { protected Object [][] getContents (); } - go on public course com . google . android . gms . common . internal . safeparcel . SafeParcelable { public static final *** Null ; } - keepnames @com . google . android . gms . mutual . notation . KeepName course * - keepclassmembernames class * { @com . google . android . gms . mutual . annotation . KeepName *; } - keepnames grade * implements android . os . Parcelable { public static final ** CREATOR ; }
6. Telephone call AdvertisingIdClient.getAdvertisingIdInfo(getApplicationContext()).getId() in a worker thread to get the id in String. as similar this
AsyncTask < Void , Void , String > job = new AsyncTask < Void , Void , String >() { @Override protected String doInBackground ( Void ... params ) { AdvertisingIdClient . Info idInfo = null ; try { idInfo = AdvertisingIdClient . getAdvertisingIdInfo ( getApplicationContext ()); } grab ( GooglePlayServicesNotAvailableException due east ) { e . printStackTrace (); } catch ( GooglePlayServicesRepairableException e ) { due east . printStackTrace (); } catch ( Exception e ) { eastward . printStackTrace (); } String advertId = null ; try { advertId = idInfo . getId (); } catch ( Exception e ){ e . printStackTrace (); } render advertId ; } @Override protected void onPostExecute ( String advertId ) { Toast . makeText ( getApplicationContext (), advertId , Toast . LENGTH_SHORT ). show (); } }; task . execute ();
Y'all create a launcher on the Dash and side menu for launching it easily. Go to Android Studio =>
Create a file android-studio.desktop
# gedit android-studio/bin/android-studio.desktop
and add some lines of entry code :-
[Desktop Entry] Version=1.0 Type=Application Name=Android Studio Exec=bash -i "/home/user/android-studio/bin/studio.sh" %f Icon=/home/user/android-studio/bin/studio.png Categories=Development;IDE; Terminal=false StartupNotify=true StartupWMClass=jetbrains-android-studio Proper name[en_GB]=android-studio.desktop
Create a android-studio.desktop icon and use it.
1 important suggestion When your device is not connecting on Linux arrangement for android development. Show the error when you utilise "# ./adb devices" (go to the SDK platform-tools/)
List of devices attached ???????????? no permissions
Enter following steps :-
- sudo killall ./adb
- sudo ./adb kill-server
- sudo ./adb start-server
- sudo ./adb devices
When this is not show devices then Create a file named /tmp/android.rules with the following contents (hex vendor numbers were taken from the vendor list page):
Connect your device on organization and find vendor number (Hexa decimal format in bold colour) using the help of this command :-
# lsusb
Double-decker 036 Device 003: ID 04e8:6860 Samsung Electronics Co., Ltd. SAMSUNG_Android
Add this line :
SUBSYSTEM == "usb" , ATTRS { idVendor }== "0bb4" , MODE = "0666"
in #gedit /tmp/android.rules file and salve
sudo cp / tmp / android . rules / etc / udev / rules . d / 51 - android . rules sudo chmod 644 / etc / udev / rules . d / 51 - android . rules sudo chown root . / etc / udev / rules . d / 51 - android . rules sudo service udev restart sudo killall ./adb
After that disconnect device and so connect to arrangement.
- sudo ./adb start-server
- sudo ./adb devices
Android framework API provides 2D cartoon APIs for unproblematic animation that does not crave major dynamic changes. In that location are two ways of implementation using these API.
i. Drawing to a View
2. Drawing on a Sheet
1.Drawing a circumvolve to View
Drawing to view is a better pick when your UI does not crave dynamic changes in the application. This can be achieved merely by extending the View class and define an onDraw() callback method.
Use the Canvas given to you lot for all your cartoon,
using various Sheet.depict…() methods (Ex: canvas.drawCircle(ten / 2, y / two, radius, pigment);). onDraw() is a callback method invoked when the view is initially drawn.
Below is a simple instance lawmaking to draw a circumvolve:-
MainActivity.java
public form MainActivity extends Activeness { @Override public void onCreate ( Package savedInstanceState ) { super . onCreate ( savedInstanceState ); setContentView ( new MyView ( this )); } public class MyView extends View { public MyView ( Context context ) { super ( context ); // TODO Auto-generated constructor stub } @Override protected void onDraw ( Sheet sail ) { // TODO Auto-generated method stub super . onDraw ( canvas ); int x = getWidth (); int y = getHeight (); int radius ; radius = 100 ; Pigment paint = new Paint (); paint . setStyle ( Paint . Style . FILL ); pigment . setColor ( Color . WHITE ); sheet . drawPaint ( pigment ); // Apply Color.parseColor to ascertain HTML colors paint . setColor ( Color . parseColor ( "#FB9J2F" )); canvas . drawCircle ( 10 / two , y / 2 , radius , paint ); } } }
ii. Cartoon rectangle on a canvas
To depict dynamic 2D graphics where in your application needs to regularly re depict itself, drawing on a canvas is a better option. A Canvas works for you as an interface, to the actual surface upon which your graphics will be fatigued.
If y'all need to create a new Canvas, then you must define the bitmap upon which cartoon will actually be performed. The Bitmap is always required for a Sail.
The beneath instance explains to draw a rectangle:-
activity_main.xml
<? xml version = "1.0" encoding = "utf-8" ?> <LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:orientation = "vertical" android:layout_width = "match_parent" android:layout_height = "match_parent" android:id = "@+id/mylayout" > </LinearLayout>
MainActivity.java
public class MainActivity extends Activity { @Override public void onCreate ( Bundle savedInstanceState ) { super . onCreate ( savedInstanceState ); setContentView ( R . layout . activity_main ); Pigment paint = new Paint (); paint . setColor ( Colour . parseColor ( "#DD4N5C" )); Bitmap bitmap = Bitmap . createBitmap ( 512 , 800 , Bitmap . Config . ARGB_8888 ); Canvas canvas = new Canvas ( bitmap ); canvas . drawRect ( 150 , 150 , 250 , 250 , pigment ); LinearLayout layout = ( LinearLayout ) findViewById ( R . id . mylayout ); layout . setBackgroundDrawable ( new BitmapDrawable ( bitmap )); } }
Extending the Application course:
Define global variables is by extending the Application class. This is the base of operations class for maintaining global awarding state.
a) Create a new class that extends Application.
public
class
GlobalVariable
extends
Application{
private
int
data=
200
;
public
int
getData(){
render
this
.data;
}
public
void
setData(
int
d){
this
.data=d;
}
}
b) Add the class to the AndroidManifest.xml file as an aspect of <awarding> tag.
<
application
android:name
=
".GlobalVariable"
... />
c) So you can access your global data from any Activity by calling getApplication().
GlobalVariable
1000 = (GlobalVariable
)getApplication();
int
data=g.getData();
– by praveen kumar verma
Source: https://developervisits.wordpress.com/category/android/
0 Response to "Draw Dynamic Circle Ontouch Android"
Post a Comment