jitpack发布_JitPack –发布您的Android库

jitpack发布

In this tutorial, we’ll demonstrate how to create your own open source application and use it as a dependency. You must have come across many open source libraries. Definitely, you would have used a few in your projects. Now it’s your turn to create one. We’ll be creating a library for vertical scrolling ViewPagers. Library name would aptly be ViewPagerVertical.

在本教程中,我们将演示如何创建自己的开源应用程序并将其用作依赖项。 您一定遇到过许多开源库。 当然,您将在项目中使用一些。 现在该创建一个了。 我们将创建一个用于垂直滚动ViewPagers的库。 库名称将适当地为ViewPagerVertical

JitPack (JitPack)

JitPack is a package repository for JVM and Android Projects. It contains the aar and dependency jars of the libraries.

JitPack是JVM和Android项目的软件包存储库。 它包含库的aar和依赖罐。

It’s a good practice to create a sample app along with the library.

与库一起创建示例应用程序是一个好习惯。

创建我们的Android库 (Creating our Android Library)

Let’s get started to create our Android Library by starting a new Android Studio project.

让我们开始一个新的Android Studio项目,以创建我们的Android库。

Choose the Tabbed Activity template, since it gives a default code for ViewPager in our activity.
Goto File | New Module | Android Library.

选择选项卡式活动模板,因为它为我们的活动中的ViewPager提供了默认代码。
转到文件| 新模块| Android库

Enter the name of the module in the following screen. Let’s see how our new module looks in the project structure.

在以下屏幕中输入模块的名称。 让我们看看我们的新模块在项目结构中的外观。

Android库项目结构 (Android Library Project Structure)

Let’s create a new java class in our viewpagervertical module.

让我们在viewpagervertical模块中创建一个新的Java类。

The code from the VerticalViewPager.java class is given below.

下面给出了VerticalViewPager.java类的代码。

package com.journaldev.viewpagervertical;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;


public class VerticalViewPager extends ViewPager {

    public VerticalViewPager(Context context) {
        super(context);
        init();
    }

    public VerticalViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        setPageTransformer(true, new VerticalPageTransformerAnimate());
        setOverScrollMode(OVER_SCROLL_NEVER);
    }

    /**
     * Swaps the X and Y coordinates of your touch event.
     */
    private MotionEvent swapXY(MotionEvent ev) {
        float width = getWidth();
        float height = getHeight();

        float newX = (ev.getY() / height) * width;
        float newY = (ev.getX() / width) * height;

        ev.setLocation(newX, newY);

        return ev;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        boolean intercepted = super.onInterceptTouchEvent(swapXY(ev));
        swapXY(ev); // return touch coordinates to original reference frame for any child views
        return intercepted;
    }

    private class VerticalPageTransformerAnimate implements PageTransformer {
        private static final float MIN_SCALE = 0.75f;

        @Override
        public void transformPage(View view, float position) {

            int pageWidth = view.getWidth();
            int pageHeight = view.getHeight();
            float alpha = 0;
            if (0 <= position && position <= 1) {
                alpha = 1 - position;
            } else if (-1 < position && position < 0) {
                float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
                float verticalMargin = pageHeight * (1 - scaleFactor) / 2;
                float horizontalMargin = pageWidth * (1 - scaleFactor) / 2;
                if (position < 0) {
                    view.setTranslationX(horizontalMargin - verticalMargin / 2);
                } else {
                    view.setTranslationX(-horizontalMargin + verticalMargin / 2);
                }

                view.setScaleX(scaleFactor);
                view.setScaleY(scaleFactor);

                alpha = position + 1;
            }

            view.setAlpha(alpha);
            view.setTranslationX(view.getWidth() * -position);
            float yPosition = position * view.getHeight();
            view.setTranslationY(yPosition);

        }
    }


    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return super.onTouchEvent(swapXY(ev));
    }
}

Note: We’ve covered VerticalViewPager in the Nested ViewPagers Tutorial.

注意:我们在“嵌套ViewPagers教程”中介绍了VerticalViewPager。

Add the Module dependency in our app module
To add the library module in our app module, go to File | Project Structure | Click the dependency Tab| Add New Module Dependency.

在我们的应用程序模块中添加模块依赖项
要将库模块添加到我们的应用模块中,请转到文件| 项目结构 单击依赖选项卡| 添加新的模块依赖项

The following dependency should be added in your build.gradle of the app module.

以下依赖项应添加到app模块的build.gradle中。

implementation project(':viewpagervertical')

Now in our MainActivity.java class and activity_main.xml of the app module, replace the ViewPager instances with VerticalViewPager.

现在,在应用模块的MainActivity.java类和activity_main.xml中,将ViewPager实例替换为VerticalViewPager

That’s it, our application is ready. Let’s check the output once before creating the library.

就是这样,我们的应用程序已准备就绪。 在创建库之前,让我们检查一次输出。

发布Android库 (Publishing Android Library)

To get our library production ready, we need to do the following changes in the Gradle.

为了准备好图书馆的产品,我们需要在Gradle中进行以下更改。

Add the following dependency to the project level root build.gradle file.

将以下依赖项添加到项目级别根build.gradle文件。

classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'

In the library module build.gradle file, add the following two lines after the top line apply plugin: 'com.android.library'.

在库模块build.gradle文件中,在apply plugin: 'com.android.library'的顶行之后添加以下两行apply plugin: 'com.android.library'

apply plugin: 'com.github.dcendents.android-maven'
group='com.github.anupamchugh'

In the group field replace the last string with your github username when you create your library.

在创建库时,在group字段中用github用户名替换最后一个字符串。

Sync Changes!

同步更改!

Go Ahead and push your project to GitHub.

继续前进,并将您的项目推送到GitHub。

You can go to VCS | Import into version control | Share Project on GitHub.

您可以转到VCS | 导入版本控制| 在GitHub上共享项目

Once it is done, create a release TAG for your repo.

完成后, 为您的仓库创建一个发布标签

Enter the following lines in the command line console present in Android Studio.

在Android Studio中提供的命令行控制台中输入以下行。

git tag -a 1.0 -m "v1.0"
git push origin 1.0

Each time you do changes in the library, you need to update the version numbers in the above lines.

每次在库中进行更改时,都需要更新上述各行中的版本号。

Typically version numbers are of form 1.0.x for minor bugs. 1.x.x for serious bugs/new features, 2.x.x for major changes.

通常,版本号的格式为1.0.x,以解决一些小错误。 1.xx表示严重的错误/新功能,2.xx表示重大更改。

Now the final stage!

现在是最后阶段!

Open JitPack, sign-in and search your repository by the url.

打开JitPack ,登录并通过URL搜索您的存储库。

In our case it looks like this:

JitPack Android Library

在我们的情况下,它看起来像这样:

Clicking on Get would show you the dependency url. Follow the instructions below.
Typically you’ll need to add the following in the root build.gradle.

单击“获取”将显示依赖项URL。 请按照以下说明进行操作。
通常,您需要在build.gradle根目录中添加以下build.gradle

allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
	}

Replace implementation ‘module_name’ in your app’s build.gradle with the following dependency.

用以下依赖项替换应用程序的build.gradle实现“ module_name”。

dependencies {
	        implementation 'com.github.anupamchugh:VerticalViewPager:1.0'
	}

That’s it. We’ve created our first Android Library.

而已。 我们已经创建了第一个Android库。

You can download the Android VerticalViewPager Project from the link below. Try modifying the VerticalViewPager and publish it as your library following the above set of instructions.

您可以从下面的链接下载Android VerticalViewPager项目 。 请按照上述说明,尝试修改VerticalViewPager并将其作为库发布。

翻译自: https://www.journaldev.com/19364/jitpack-publish-android-library

jitpack发布

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值