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

时间:2018-11-28 07:48:47

标签: android android-layout

我想在图像视图的图像左上方放置一个徽标。无法缩放图像或无法分配缩放类型。图片应为原始大小。在所有屏幕上,徽标应始终位于图像的左上方。

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));

    }

自定义适配器

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项

<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 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>

现在,编写代码的方式是将所有屏幕尺寸都硬编码到代码中,并针对每个屏幕尺寸调整徽标。图片尺寸均相同(1080像素x 1620像素)。但这对于进入市场的新设备来说不是可扩展的方法。

1 个答案:

答案 0 :(得分:2)

您可以尝试在主图像上使用adjustViewBounds,并将其徽标alignTop设置在RelativeLayout中:

<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>