Giter Site home page Giter Site logo

chaitanya79 / mylittlecanvas Goto Github PK

View Code? Open in Web Editor NEW

This project forked from florent37/mylittlecanvas

0.0 1.0 0.0 412 KB

You find canvas hard to use ? try MyLittleCanvas :) Don't work with canvas methods, use objects instead !

License: Apache License 2.0

Java 100.00%

mylittlecanvas's Introduction

MyLittleCanvas

You find canvas hard to use ? try MyLittleCanvas :) Don't work with canvas methods, use objects instead !

For example, to add a custom underline on a textview

gif

public class UnderlinedTextView extends android.support.v7.widget.AppCompatTextView {

    //define your canvas shapes
    final RoundRectShape roundRectShape = new RoundRectShape();

    public UnderlinedTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        roundRectShape
                .setCorderRadius(10)
                .setColor(Color.parseColor("#3F51B5"));
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        //customize your shapes
        roundRectShape
                .setWidth(w)
                .setHeight(10)
                .alignBottom(h);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //and finally draw
        roundRectShape.onDraw(canvas);
    }
}

Buy Me a Coffee at ko-fi.com

Download

dependencies {
    compile 'com.github.florent37:mylittlecanvas:(lastversion)'
}

Implementation

Shapes

  • RectShape
  • RoundRectShape
  • TextShape
  • LineShape
  • DrawableShape
  • PathShape
  • CircleShape

Methods

.setColor(int)
.setWillNotDraw(boolean)
.getCenterX() / .getCenterY()
.getHeight() / .getWidth()

//rect / roundRect
.setRect(left, top, left, bottom)
.setLeft/Right/Top/Bottom(value)
.alignLeft/Right/Top/Bottom(value / shape)
.below(shape)
.above(shape)
.setCorderRadius(corner)
.marginLeft/Right/Top/Bottom(value / shape)

//text
.setText(text)
.setTextSizePx(size)
.setTypeface(typeface)
.centerIn(rectShape)
.configureH(minX, maxX)
.centerVertical(minY, maxY)

Graph Sample

gif

Complete file, with all shaped and motion event manager

https://github.com/florent37/MyLittleCanvas/blob/master/app/src/main/java/canvastoolbox/florent37/github/com/canvastoolbox/MyTreeView.java

public class MyTreeView extends View {

    //define your canvas shapes
    ...
    RoundRectShape childLeft;
    TextShape textChildLeft;
    LineShape lineParentChildLeft;
    ...

    public MyTreeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);

            //customize your shapes

            ...
            childLeft.setCorderRadius(10)
                    .setColor(Color.parseColor("#2196F3"));
            textChildLeft.setColor(Color.WHITE)
                    .setTextSizePx(40);
            lineParentChildLeft.setStrokeWith(3)
                    .setColor(Color.parseColor("#3E3E3E"));
            ...
    }

    @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);

            //setup your shapes
            childLeft.setLeft(40)
                            .setWidth(200)
                            .below(parent) //another shape
                            .marginTop(250)
                            .setHeight(100);

            textChildLeft.setText("childLeft");
            textChildLeft
                    .setAlignment(Layout.Alignment.ALIGN_CENTER)
                    .centerIn(childLeft);

            lineParentChildLeft
                    .start(parent.getCenterX(), parent.getBottom())
                    .end(childLeft.getCenterX(), childLeft.getTop());
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);

            ...
            //draw your shapes
            childLeft.onDraw(canvas);
            textChildLeft.onDraw(canvas);
            lineParentChildLeft.onDraw(canvas);
            ...
        }
}

Credits

Author: Florent Champigny http://www.florentchampigny.com/

Blog : http://www.tutos-android-france.com/

Android app on Google Play Follow me on Google+ Follow me on Twitter Follow me on LinkedIn

License

Copyright 2017 Florent37, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

mylittlecanvas's People

Contributors

florent37 avatar

Watchers

 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.