Coder Social home page Coder Social logo

imageviewwithpicaso's Introduction

ImageViewWIthPicaso

Picasso Dependencies

implementation 'com.squareup.picasso:picasso:2.5.1'

Add ImageView in Design

<ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

We have two methods one for loading without rotation & other for with rotation

public class MainActivity extends AppCompatActivity {

    ImageView imageView;
    String url = "https://i.pinimg.com/564x/b2/45/37/b24537a5a41ca67dacfec5f3c2655ef9.jpg";
    Float angle = 0f;
    
    private void loadImage(String url, Float rotation, ImageView imageView) {
        Picasso.with(this)
                .load(url)
                .resize(1200, 1400)
                .rotate(rotation)
                .into(imageView);
    }

    private void loadImageWithRotation(String url, Float rotation, ImageView imageView) {
        angle += rotation;
        Picasso.with(this)
                .load(url)
                .resize(1200, 1400)
                .rotate(angle)
                .into(imageView);
    }
}

Adding Menus

Change theme in AndroidManifest.xml

 android:theme="@style/Theme.AppCompat.Light.NoActionBar"

Add Menu in XML file

Create a "menu" folder under res directory. So your folder structure will look like res/menu. Under menu folder, create xml file with menu items

res/menu/my_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/rotate_right"
        android:icon="@drawable/rotate_right"
        android:title="right"
        app:showAsAction="ifRoom"/>

    <item
        android:id="@+id/rotate_left"
        android:icon="@drawable/rotate_left"
        android:title="left"
        app:showAsAction="ifRoom"/>
        
    <item android:id="@+id/action_settings"
        android:title="settings"
        app:showAsAction="never"/>
</menu>

Create Menu in activity_main.xml

activity_main.xml

<android.support.v7.widget.Toolbar
        android:id="@+id/my_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

Add following code in MainActivity.java to display menu bar

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
    setSupportActionBar(myToolbar);
 
    // Remaining code will follow
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.my_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

    case R.id.rotate_right:
      loadImageWithRotation(url, 90f, imageView);
       return true;
    case R.id.rotate_left:
       loadImageWithRotation(url, 270f, imageView);
        return true;
  }
  return (super.onOptionsItemSelected(item));
}

Reference

For Logos - https://material.io/tools/icons/?icon=rotate_right&style=baseline For App Bar - https://developer.android.com/training/appbar/

imageviewwithpicaso's People

Contributors

sathya26d94 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.