简体   繁体   English

在图像视图中将徽标对准图像左上方

[英]Align logo on top left of Image in image view

I want to place a logo on top left of image inside image view. 我想在图像视图的图像左上方放置一个徽标。 Image cannot be scaled or no scale type can be assigned. 无法缩放图像或无法分配缩放类型。 Image should be of its original size. 图片应为原始大小。 The logo should always be on top left of the image on all screens. 在所有屏幕上,徽标应始终位于图像的左上方。

MainActivity 主要活动

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_main);

        ViewPager viewPager = findViewById(R.id.imgviewPager);

        List<String> imagesList = new ArrayList<>();
        imagesList.add("http://images.unsplash.com/photo-1503919545889-aef636e10ad4?ixlib=rb-0.3.5&q=80&fm=jpg");
        imagesList.add("http://images.unsplash.com/photo-1533738699159-d0c68059bb61?ixlib=rb-0.3.5&q=80&fm=jpg");
        imagesList.add("http://images.unsplash.com/photo-1500462918059-b1a0cb512f1d?ixlib=rb-0.3.5&q=80&fm=jpg");
        imagesList.add("http://images.unsplash.com/photo-1516474504835-58236f241ee8?ixlib=rb-0.3.5&q=80&fm=jpg");


        viewPager.setAdapter(new CustomPagerAdapter(this,imagesList));

    }

Custom Adapter 定制适配器

class CustomPagerAdapter extends PagerAdapter {

    private Context mContext;
    private List<String> imgs;

    public CustomPagerAdapter(Context context,List<String> img) {
        mContext = context;
        imgs = img;
    }

    @Override
    public Object instantiateItem(ViewGroup collection, int position) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.image_item, collection, false);
        collection.addView(layout);
        ImageView imageView = layout.findViewById(R.id.img);
        Picasso.get().load(imgs.get(position)).into(imageView);

        return layout;
    }

    @Override
    public void destroyItem(ViewGroup collection, int position, Object view) {
        collection.removeView((View) view);
    }

    @Override
    public int getCount() {
        return imgs.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return "";
    }

ImageAdapter item ImageAdapter项目

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center|bottom">
        <ImageView
            android:id="@+id/img"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <ImageView
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:src="@mipmap/ic_launcher"/>

    </RelativeLayout>

</LinearLayout>

Style 样式

<style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowAnimationStyle">@null</item>
        <item name="android:windowDrawsSystemBarBackgrounds" tools:ignore="NewApi">true</item>
        <item name="android:statusBarColor" tools:ignore="NewApi">@android:color/transparent</item>
    </style>

Right now the code is written such a way that all the screen sizes are hard coded into the code and logo is adjusted for each screen size. 现在,编写代码的方式是将所有屏幕尺寸都硬编码到代码中,并针对每个屏幕尺寸调整徽标。 Images sizes are all the same (1080px x 1620px). 图片尺寸均相同(1080像素x 1620像素)。 But this is not scalable approach for the new devices thats coming into the market. 但这对于即将投放市场的新设备而言,这不是可扩展的方法。

You could try to use adjustViewBounds on the main image, and set your logo alignTop of it in a RelativeLayout : 您可以尝试在主图像上使用adjustViewBounds ,并在RelativeLayout 中将其徽标alignTop设置为:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <ImageView
            android:id="@+id/img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:adjustViewBounds="true"/>
    <ImageView
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_alignTop="@+id/img"
            android:src="@mipmap/ic_launcher"/>
</RelativeLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM