GVKun编程网logo

如何在 swift 中在自定义导航栏顶部制作一半的 ImageView(swiftui自定义导航栏)

6

本文将介绍如何在swift中在自定义导航栏顶部制作一半的ImageView的详细情况,特别是关于swiftui自定义导航栏的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题

本文将介绍如何在 swift 中在自定义导航栏顶部制作一半的 ImageView的详细情况,特别是关于swiftui自定义导航栏的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同时也将涉及一些关于android 实现圆形 imageView,Circle imageView.、Android,ImageView over ImageView、Android:如何将 ImageView 的内容调整为与另一个 ImageView 内容相同的尺寸?、FitWidth ImageView 和 TopCrop ImageView的知识。

本文目录一览:

如何在 swift 中在自定义导航栏顶部制作一半的 ImageView(swiftui自定义导航栏)

如何在 swift 中在自定义导航栏顶部制作一半的 ImageView(swiftui自定义导航栏)

如何解决如何在 swift 中在自定义导航栏顶部制作一半的 ImageView

我正在为所有视图控制器使用自定义导航栏..但是在我的一个视图控制器中,我需要导航栏顶部的一半图像,该怎么做?请帮忙

像这样我需要

enter image description here

对于设计,我使用故事板, 为此,我在 uiview 中拍摄了一个图像视图

uiview 约束

 top = 0,leading = 0,trailing = 0,height = 100

图像视图约束

 top = -40,horizontal center,height,width = 80

现在我变成这样了

enter image description here

我的自定义导航栏代码:请帮助我在一个视图控制器中的导航栏顶部制作图像视图

 func updateNavigationBar(with title : String,isTransparent : Bool = false,isBackNeed : Bool = true,isNotificationNeed : Bool = true,isMenuNeed : Bool = true){
    self.navigationController?.interactivePopGestureRecognizer?.delegate = nil
    self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
    if isTransparent{
        self.navigationController?.navigationBar.setBackgroundImage(UIImage(),for: .default)
        self.navigationController?.navigationBar.shadowImage = UIImage()
        self.navigationController?.navigationBar.backgroundColor = .clear
        self.navigationController?.navigationBar.isTranslucent = true
        self.navigationController?.navigationBar.layer.shadowColor = UIColor.black.cgColor
        self.navigationController?.navigationBar.layer.shadowOpacity = 0
        self.navigationController?.navigationBar.layer.shadowOffset = CGSize.zero
        self.navigationController?.navigationBar.layer.shadowRadius = 0
    }else{
        self.navigationController?.navigationBar.shadowImage = UIImage()
        self.navigationController?.navigationBar.backgroundColor = CommonColor.navigationColor
        self.navigationController?.navigationBar.barTintColor = CommonColor.navigationColor
        self.navigationController?.navigationBar.isTranslucent = false
        self.navigationController?.navigationBar.layer.masksToBounds = false
        self.navigationController?.navigationBar.layer.shadowColor = UIColor.black.cgColor
        self.navigationController?.navigationBar.layer.shadowOpacity = 0.2
        self.navigationController?.navigationBar.layer.shadowOffset = CGSize(width: 0,height: 2.0)
        self.navigationController?.navigationBar.layer.shadowRadius = 2
    }
    let btnTitle = UIBarButtonItem(title: title,style: .plain,target: self,action: #selector(backClick))
    btnTitle.tintColor = .white
    
    let backImageView = UIImageView.init(image: CommonImage.back?.withRenderingMode(.alwaystemplate))
    backImageView.tintColor = .white
    backImageView.contentMode = .scaleAspectFit
    backImageView.frame = CGRect(x:0.0,y:0.0,width:20,height:25.0)
    backImageView.contentMode = .scaleAspectFit
    backImageView.widthAnchor.constraint(equalToConstant: 20).isActive = true
    backImageView.heightAnchor.constraint(equalToConstant: 25).isActive = true
    backImageView.addGestureRecognizer(UITapGestureRecognizer(target: self,action: #selector(backClick)))
    let btnBack = UIBarButtonItem(customView: backImageView)
    
    let menuImageView = UIImageView.init(image: CommonImage.menu?.withRenderingMode(.alwaystemplate))
    menuImageView.tintColor = .white
    menuImageView.contentMode = .scaleAspectFit
    menuImageView.frame = CGRect(x:0.0,width:25,height:25.0)
    menuImageView.contentMode = .scaleAspectFit
    menuImageView.widthAnchor.constraint(equalToConstant: 25).isActive = true
    menuImageView.heightAnchor.constraint(equalToConstant: 25).isActive = true
    menuImageView.addGestureRecognizer(UITapGestureRecognizer(target: self,action: #selector(menuClick)))
    let btnMenu = UIBarButtonItem(customView: menuImageView)
    
    let notificatonView = UIView()
    notificatonView.frame = CGRect(x:0.0,width:32,height:32.0)
    notificatonView.layer.cornerRadius = 8
    notificatonView.alpha = 0.5
    notificatonView.backgroundColor = .white
    notificatonView.clipsToBounds = true
    notificatonView.widthAnchor.constraint(equalToConstant: 32).isActive = true
    notificatonView.heightAnchor.constraint(equalToConstant: 32).isActive = true
    notificatonView.addGestureRecognizer(UITapGestureRecognizer(target: self,action: #selector(notificationClick)))
    
    let notificationBtn: BadgeButton = BadgeButton(type: .custom)
    notificationBtn.setimage(CommonImage.notification,for: .normal)
    notificationBtn.addAction(for: .touchUpInside) {
        
    }
    notificationBtn.frame = CGRect(x: 0,y: 0,width: 30,height: 30)
    notificationBtn.badgeBackgroundColor = CommonColor.navigationColor
    notificationBtn.badgeTextColor = .white
    if notificatonView.subviews.filter({$0 is BadgeButton}).count == 0{
        notificatonView.addSubview(notificationBtn)
        notificationBtn.center = notificatonView.center
    }
    let notificationBarButton = UIBarButtonItem(customView: notificatonView)
    if isBackNeed{
        self.navigationItem.setLeftBarButtonItems([btnBack,btnTitle],animated: true)
        self.navigationItem.setRightBarButtonItems( isNotificationNeed ? [notificationBarButton,btnMenu] : [notificationBarButton],animated: true)
    }else{
        self.navigationItem.setLeftBarButtonItems( isMenuNeed ? [btnMenu,btnTitle] : [btnTitle],animated: true)
        self.navigationItem.setRightBarButtonItems( isNotificationNeed ? [notificationBarButton] : nil,animated: true)
    }
}

如何在导航栏顶部显示一半的图像视图,请帮忙

解决方法

您需要创建自定义导航控制器,以便我们能够实现此类设计。

,

您需要使用这种方法enter image description here

android 实现圆形 imageView,Circle imageView.

android 实现圆形 imageView,Circle imageView.

OSC 请你来轰趴啦!1028 苏州源创会,一起寻宝 AI 时代

    设计给出了一个圆圈形显示网络图片的方案,无奈,程序猿只好写了一个,现共享出来,代码很简单。

   类直接继承自 ImageView,修改下他的 canvas 就行了;

package cn.helloclq.android.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Path;
import android.graphics.Region;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * 
 * android circle imageView
 * @author Block Cheng
 *
 */
public class CircleImageView extends ImageView {

	Path path;
	public PaintFlagsDrawFilter mPaintFlagsDrawFilter;// 毛边过滤
	Paint paint;
	
	public CircleImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		init();
	}

	public CircleImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		init();
	}

	public CircleImageView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init();
	}
	public void init(){
		mPaintFlagsDrawFilter = new PaintFlagsDrawFilter(0,
				Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG);
		paint = new Paint();
		paint.setAntiAlias(true);
		paint.setFilterBitmap(true);
		paint.setColor(Color.WHITE);
		
	}
	
	@Override
	protected void onDraw(Canvas cns) {
		// TODO Auto-generated method stub
		float h = getMeasuredHeight()- 3.0f;
		float w = getMeasuredWidth()- 3.0f;
		if (path == null) {
			path = new Path();
	        path.addCircle(
	        		w/2.0f
	                , h/2.0f
	                , (float) Math.min(w/2.0f, (h / 2.0))
	                , Path.Direction.CCW);
	        path.close();
		}
		cns.drawCircle(w/2.0f, h/2.0f,  Math.min(w/2.0f, h / 2.0f) + 1.5f, paint);
		 int saveCount = cns.getSaveCount();
        cns.save();
        cns.setDrawFilter(mPaintFlagsDrawFilter);
        cns.clipPath(path,Region.Op.REPLACE);
        cns.setDrawFilter(mPaintFlagsDrawFilter);
        cns.drawColor(Color.WHITE);
		super.onDraw(cns);
		cns.restoreToCount(saveCount);
	}
	
}
原图片:

放到代码中测试运行

说明:该类对图片的裁剪,导致了轻微的毛边出来,采用网络上的方法处理了,效果不佳,详见代码中的 filter。

但如果服务器的图片是纯色背景,这个问题就不是问题了。我也不深入研究了,后面还有很重的任务。

另外,裁剪出圆形图片也是利用相似的原理实现。

如果哪位朋友有好的解决方法,欢迎分享给我,万分谢谢。

Android,ImageView over ImageView

Android,ImageView over ImageView

我有一个非常简单的问题要问:我需要在屏幕右下方的Im​​ageView上放一个小徽标,大屏幕,但我不知道如何设置坐标或怎么说ImageViews处于相对位置.

像这样的东西:

解决方法:

尝试这个

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

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

产量

Android:如何将 ImageView 的内容调整为与另一个 ImageView 内容相同的尺寸?

Android:如何将 ImageView 的内容调整为与另一个 ImageView 内容相同的尺寸?

如何解决Android:如何将 ImageView 的内容调整为与另一个 ImageView 内容相同的尺寸?

我正在尝试将 ImageView 的内容调整为与另一个 ImageView 的内容相同的尺寸。例如,假设我有以下两张图片,其中一张是水平方向的,另一张是垂直方向的:

垂直图像

enter image description here

水平图像

enter image description here

我想将垂直图像调整为水平图像,如下所示:

enter image description here

最重要的是,ImageViews 的尺寸和它们的内容必须相同,如下面的横向 ImageView 截图所示:

enter image description here

我尝试为水平 BitMap 检索 ImageView 的尺寸并将垂直 ImageViewBitMap 重新缩放到水平 BitMap,但是为 BitMap 检索的尺寸始终显示原始水平照片的尺寸,而不是缩放以适合水平 ImageView 的照片尺寸。

以下是我迄今为止一直在工作的代码:

activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent">
  7. <FrameLayout
  8. android:id="@+id/slider_screen_photo_layout"
  9. android:layout_width="0dp"
  10. android:layout_height="wrap_content"
  11. android:orientation="vertical"
  12. android:padding="5dip"
  13. app:layout_constraintEnd_toEndOf="parent"
  14. app:layout_constraintStart_toStartOf="parent"
  15. app:layout_constraintTop_toTopOf="parent"
  16. tools:ignore="MissingConstraints">
  17. <ImageView
  18. android:id="@+id/slider_screen_image_1_Box"
  19. android:layout_width="match_parent"
  20. android:layout_height="wrap_content"
  21. android:adjustViewBounds="true"
  22. android:maxHeight="370dp"
  23. android:scaleType="fitStart"
  24. app:layout_constraintEnd_toEndOf="parent"
  25. app:layout_constraintStart_toStartOf="parent"
  26. app:layout_constraintTop_toTopOf="parent" />
  27. <ImageView
  28. android:id="@+id/slider_screen_image_2_Box"
  29. android:layout_width="match_parent"
  30. android:layout_height="wrap_content"
  31. android:adjustViewBounds="true"
  32. android:maxHeight="370dp"
  33. android:scaleType="matrix"
  34. app:layout_constraintEnd_toEndOf="parent"
  35. app:layout_constraintStart_toStartOf="parent"
  36. app:layout_constraintTop_toTopOf="parent" />
  37. </FrameLayout>
  38. </androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java(检索横向 ImageView 的位图尺寸)

  1. ImageView sliderPreviewBox = null;
  2. if (photoNum == 1) {
  3. sliderPreviewBox = findViewById(R.id.slider_screen_image_1_Box);
  4. }
  5. if (photoNum == 2) {
  6. sliderPreviewBox = findViewById(R.id.slider_screen_image_2_Box);
  7. }
  8. Drawable drawable = sliderPreviewBox.getDrawable();
  9. int width = drawable.getIntrinsicWidth();
  10. int height = drawable.getIntrinsicHeight();

解决方法

试试这个:

onCreate..

  1. Bitmap bitmap = BitmapFactory.decodeFile(MyPath);
  2. try {
  3. int rotation = getExifOrientation(MyPath);
  4. Log.d(TAG,"\\n--- Rotation of the image " + name + " is: " + rotation);
  5. } catch (IOException e) {
  6. e.printStackTrace();
  7. }
  8. Bitmap rot_bitmap = rotateBitmap(MyPath,bitmap);
  9. imageView.setImageBitmap(rot_bitmap);

之后:

  1. // To get the original orientation of the image
  2. public static Bitmap rotateBitmap(String src,Bitmap bitmap) {
  3. try {
  4. // Getting the orientation of the image,there are 6 cases:
  5. // The Exif specification defines an Orientation Tag to indicate the orientation of the camera relative to the captured scene
  6. // Source: http://sylvana.net/jpegcrop/exif_orientation.html
  7. int rotation = getExifOrientation(src);
  8. if (rotation == 1) { return bitmap; }
  9. Matrix matrix = new Matrix();
  10. switch (rotation) {
  11. case 2: matrix.setScale(-1,1); break;
  12. case 3: matrix.setRotate(180); break;
  13. case 4: matrix.setRotate(180); matrix.postScale(-1,1); break;
  14. case 5: matrix.setRotate(90); matrix.postScale(-1,1); break;
  15. case 6: matrix.setRotate(90); break;
  16. case 7: matrix.setRotate(-90); matrix.postScale(-1,1); break;
  17. case 8: matrix.setRotate(-90); break;
  18. default: return bitmap;
  19. }
  20. // Creating a bitmap after its rotation and recycling the original one
  21. try {
  22. Bitmap oriented = Bitmap.createBitmap(bitmap,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
  23. // on low-memory system such as Android,must be attention to add bitmap.recycle(); to avoid the memory leak exception
  24. bitmap.recycle();
  25. return oriented;
  26. } catch (OutOfMemoryError e) {
  27. e.printStackTrace();
  28. return bitmap;
  29. }
  30. } catch (IOException e) {
  31. e.printStackTrace();
  32. }
  33. return bitmap;
  34. }
  35. // function to get the orientation of bitmap
  36. private static int getExifOrientation(String src) throws IOException {
  37. int orientation = 1;
  38. ExifInterface exif = new ExifInterface(src);
  39. String orientationString=exif.getAttribute(ExifInterface.TAG_ORIENTATION);
  40. try { assert orientationString != null;
  41. orientation = Integer.parseInt(orientationString); }
  42. catch(NumberFormatException e) { e.printStackTrace(); }
  43. return orientation;
  44. }

FitWidth ImageView 和 TopCrop ImageView

FitWidth ImageView 和 TopCrop ImageView

FitWidth ImageView: 宽度自适应

[html]  view plain copy print ?
  1. <com.kk.drama.view.widget.FitWidthImageView  
  2.           android:id="@+id/show_images"  
  3.           android:layout_width="match_parent"  
  4.           android:layout_height="match_parent"  
  5.           android:src="@drawable/default_picture" />  



[java]  view plain copy print ?
  1. public class FitWidthImageView extends ImageView  
  2. {  
  3.      
  4.     public FitWidthImageView(Context context) {  
  5.         super(context);  
  6.         setup();  
  7.     }  
  8.      
  9.     public FitWidthImageView(Context context, AttributeSet attrs) {  
  10.         super(context, attrs);  
  11.         setup();  
  12.     }  
  13.      
  14.     public FitWidthImageView(Context context, AttributeSet attrs, int defStyle) {  
  15.         super(context, attrs, defStyle);  
  16.         setup();  
  17.     }  
  18.      
  19.     @Override  
  20.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  21.         int width = MeasureSpec.getSize(widthMeasureSpec);  
  22.         int height = width * getDrawable().getIntrinsicHeight() / getDrawable().getIntrinsicWidth();  
  23.         setMeasuredDimension(width, height);  
  24.     }  
  25.      
  26.     private void setup() {  
  27.         setScaleType(ScaleType.CENTER_CROP);  
  28.     }  
  29.   
  30. }  



TopCrop ImageView : 从头部 Crop 而不是 center

 
[html]  view plain copy print ?
  1. <com.kk.drama.view.widget.FitWidthImageView  
  2.             android:id="@+id/show_images"  
  3.             android:layout_width="match_parent"  
  4.             android:layout_height="227dp"  
  5.             android:src="@drawable/default_picture" />  

自己改名
[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. public class FitWidthImageView extends ImageView  
  2. {  
  3.      
  4.     public FitWidthImageView(Context context) {  
  5.         super(context);  
  6.         setup();  
  7.     }  
  8.      
  9.     public FitWidthImageView(Context context, AttributeSet attrs) {  
  10.         super(context, attrs);  
  11.         setup();  
  12.     }  
  13.      
  14.     public FitWidthImageView(Context context, AttributeSet attrs, int defStyle) {  
  15.         super(context, attrs, defStyle);  
  16.         setup();  
  17.     }  
  18.      
  19.     private void setup() {  
  20.         setScaleType(ScaleType.CENTER_CROP);  
  21.         setScaleType(ScaleType.MATRIX);  
  22.     }  
  23.      
  24.     @Override  
  25.     protected boolean setFrame(int frameLeft, int frameTop, int frameRight, int frameBottom) {  
  26.         float frameWidth = frameRight - frameLeft;  
  27.         float frameHeight = frameBottom - frameTop;  
  28.          
  29.         float originalImageWidth = (float) getDrawable().getIntrinsicWidth();  
  30.         float originalImageHeight = (float) getDrawable().getIntrinsicHeight();  
  31.          
  32.         float usedScaleFactor = 1;  
  33.          
  34.         if ((frameWidth > originalImageWidth) || (frameHeight > originalImageHeight)) {  
  35.             // If frame is bigger than image  
  36.             // => Crop it, keep aspect ratio and position it at the bottom and center horizontally  
  37.              
  38.             float fitHorizontallyScaleFactor = frameWidth / originalImageWidth;  
  39.             float fitVerticallyScaleFactor = frameHeight / originalImageHeight;  
  40.              
  41.             usedScaleFactor = Math.max(fitHorizontallyScaleFactor, fitVerticallyScaleFactor);  
  42.         }  
  43.          
  44.         float newImageWidth = originalImageWidth * usedScaleFactor;  
  45.         float newImageHeight = originalImageHeight * usedScaleFactor;  
  46.          
  47.         Matrix matrix = getImageMatrix();  
  48.         matrix.setScale(usedScaleFactor, usedScaleFactor, 00); // Replaces the old matrix completly  
  49.         // matrix.postTranslate((frameWidth - newImageWidth) / 2, frameHeight - newImageHeight);//BottomCrop  
  50.         matrix.postTranslate((frameWidth - newImageWidth) / 20);//Top Crop  
  51.         setImageMatrix(matrix);  
  52.         return super.setFrame(frameLeft, frameTop, frameRight, frameBottom);  
  53.     }  
  54. }  

今天关于如何在 swift 中在自定义导航栏顶部制作一半的 ImageViewswiftui自定义导航栏的分享就到这里,希望大家有所收获,若想了解更多关于android 实现圆形 imageView,Circle imageView.、Android,ImageView over ImageView、Android:如何将 ImageView 的内容调整为与另一个 ImageView 内容相同的尺寸?、FitWidth ImageView 和 TopCrop ImageView等相关知识,可以在本站进行查询。

本文标签: