本文将介绍如何在swift中在自定义导航栏顶部制作一半的ImageView的详细情况,特别是关于swiftui自定义导航栏的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题
本文将介绍如何在 swift 中在自定义导航栏顶部制作一半的 ImageView的详细情况,特别是关于swiftui自定义导航栏的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同时也将涉及一些关于android 实现圆形 imageView,Circle imageView.、Android,ImageView over ImageView、Android:如何将 ImageView 的内容调整为与另一个 ImageView 内容相同的尺寸?、FitWidth ImageView 和 TopCrop ImageView的知识。
本文目录一览:- 如何在 swift 中在自定义导航栏顶部制作一半的 ImageView(swiftui自定义导航栏)
- android 实现圆形 imageView,Circle imageView.
- Android,ImageView over ImageView
- Android:如何将 ImageView 的内容调整为与另一个 ImageView 内容相同的尺寸?
- FitWidth ImageView 和 TopCrop ImageView
如何在 swift 中在自定义导航栏顶部制作一半的 ImageView(swiftui自定义导航栏)
如何解决如何在 swift 中在自定义导航栏顶部制作一半的 ImageView
我正在为所有视图控制器使用自定义导航栏..但是在我的一个视图控制器中,我需要导航栏顶部的一半图像,该怎么做?请帮忙
像这样我需要
对于设计,我使用故事板, 为此,我在 uiview 中拍摄了一个图像视图
uiview 约束
top = 0,leading = 0,trailing = 0,height = 100
图像视图约束
top = -40,horizontal center,height,width = 80
现在我变成这样了
我的自定义导航栏代码:请帮助我在一个视图控制器中的导航栏顶部制作图像视图
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)
}
}
如何在导航栏顶部显示一半的图像视图,请帮忙
解决方法
您需要创建自定义导航控制器,以便我们能够实现此类设计。
,您需要使用这种方法
android 实现圆形 imageView,Circle imageView.

设计给出了一个圆圈形显示网络图片的方案,无奈,程序猿只好写了一个,现共享出来,代码很简单。
类直接继承自 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
我有一个非常简单的问题要问:我需要在屏幕右下方的ImageView上放一个小徽标,大屏幕,但我不知道如何设置坐标或怎么说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 内容相同的尺寸?
我正在尝试将 ImageView
的内容调整为与另一个 ImageView
的内容相同的尺寸。例如,假设我有以下两张图片,其中一张是水平方向的,另一张是垂直方向的:
垂直图像:
水平图像:
我想将垂直图像调整为水平图像,如下所示:
最重要的是,ImageViews
的尺寸和它们的内容必须相同,如下面的横向 ImageView
截图所示:
我尝试为水平 BitMap
检索 ImageView
的尺寸并将垂直 ImageView
的 BitMap
重新缩放到水平 BitMap
,但是为 BitMap
检索的尺寸始终显示原始水平照片的尺寸,而不是缩放以适合水平 ImageView
的照片尺寸。
以下是我迄今为止一直在工作的代码:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/slider_screen_photo_layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dip"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints">
<ImageView
android:id="@+id/slider_screen_image_1_Box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxHeight="370dp"
android:scaleType="fitStart"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/slider_screen_image_2_Box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxHeight="370dp"
android:scaleType="matrix"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java(检索横向 ImageView 的位图尺寸):
ImageView sliderPreviewBox = null;
if (photoNum == 1) {
sliderPreviewBox = findViewById(R.id.slider_screen_image_1_Box);
}
if (photoNum == 2) {
sliderPreviewBox = findViewById(R.id.slider_screen_image_2_Box);
}
Drawable drawable = sliderPreviewBox.getDrawable();
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
解决方法
试试这个:
onCreate..
Bitmap bitmap = BitmapFactory.decodeFile(MyPath);
try {
int rotation = getExifOrientation(MyPath);
Log.d(TAG,"\\n--- Rotation of the image " + name + " is: " + rotation);
} catch (IOException e) {
e.printStackTrace();
}
Bitmap rot_bitmap = rotateBitmap(MyPath,bitmap);
imageView.setImageBitmap(rot_bitmap);
之后:
// To get the original orientation of the image
public static Bitmap rotateBitmap(String src,Bitmap bitmap) {
try {
// Getting the orientation of the image,there are 6 cases:
// The Exif specification defines an Orientation Tag to indicate the orientation of the camera relative to the captured scene
// Source: http://sylvana.net/jpegcrop/exif_orientation.html
int rotation = getExifOrientation(src);
if (rotation == 1) { return bitmap; }
Matrix matrix = new Matrix();
switch (rotation) {
case 2: matrix.setScale(-1,1); break;
case 3: matrix.setRotate(180); break;
case 4: matrix.setRotate(180); matrix.postScale(-1,1); break;
case 5: matrix.setRotate(90); matrix.postScale(-1,1); break;
case 6: matrix.setRotate(90); break;
case 7: matrix.setRotate(-90); matrix.postScale(-1,1); break;
case 8: matrix.setRotate(-90); break;
default: return bitmap;
}
// Creating a bitmap after its rotation and recycling the original one
try {
Bitmap oriented = Bitmap.createBitmap(bitmap,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
// on low-memory system such as Android,must be attention to add bitmap.recycle(); to avoid the memory leak exception
bitmap.recycle();
return oriented;
} catch (OutOfMemoryError e) {
e.printStackTrace();
return bitmap;
}
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
// function to get the orientation of bitmap
private static int getExifOrientation(String src) throws IOException {
int orientation = 1;
ExifInterface exif = new ExifInterface(src);
String orientationString=exif.getAttribute(ExifInterface.TAG_ORIENTATION);
try { assert orientationString != null;
orientation = Integer.parseInt(orientationString); }
catch(NumberFormatException e) { e.printStackTrace(); }
return orientation;
}
FitWidth ImageView 和 TopCrop ImageView
- <com.kk.drama.view.widget.FitWidthImageView
- android:id="@+id/show_images"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:src="@drawable/default_picture" />
- public class FitWidthImageView extends ImageView
- {
- public FitWidthImageView(Context context) {
- super(context);
- setup();
- }
- public FitWidthImageView(Context context, AttributeSet attrs) {
- super(context, attrs);
- setup();
- }
- public FitWidthImageView(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- setup();
- }
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- int width = MeasureSpec.getSize(widthMeasureSpec);
- int height = width * getDrawable().getIntrinsicHeight() / getDrawable().getIntrinsicWidth();
- setMeasuredDimension(width, height);
- }
- private void setup() {
- setScaleType(ScaleType.CENTER_CROP);
- }
- }
- <com.kk.drama.view.widget.FitWidthImageView
- android:id="@+id/show_images"
- android:layout_width="match_parent"
- android:layout_height="227dp"
- android:src="@drawable/default_picture" />

- public class FitWidthImageView extends ImageView
- {
- public FitWidthImageView(Context context) {
- super(context);
- setup();
- }
- public FitWidthImageView(Context context, AttributeSet attrs) {
- super(context, attrs);
- setup();
- }
- public FitWidthImageView(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- setup();
- }
- private void setup() {
- setScaleType(ScaleType.CENTER_CROP);
- setScaleType(ScaleType.MATRIX);
- }
- @Override
- protected boolean setFrame(int frameLeft, int frameTop, int frameRight, int frameBottom) {
- float frameWidth = frameRight - frameLeft;
- float frameHeight = frameBottom - frameTop;
- float originalImageWidth = (float) getDrawable().getIntrinsicWidth();
- float originalImageHeight = (float) getDrawable().getIntrinsicHeight();
- float usedScaleFactor = 1;
- if ((frameWidth > originalImageWidth) || (frameHeight > originalImageHeight)) {
- // If frame is bigger than image
- // => Crop it, keep aspect ratio and position it at the bottom and center horizontally
- float fitHorizontallyScaleFactor = frameWidth / originalImageWidth;
- float fitVerticallyScaleFactor = frameHeight / originalImageHeight;
- usedScaleFactor = Math.max(fitHorizontallyScaleFactor, fitVerticallyScaleFactor);
- }
- float newImageWidth = originalImageWidth * usedScaleFactor;
- float newImageHeight = originalImageHeight * usedScaleFactor;
- Matrix matrix = getImageMatrix();
- matrix.setScale(usedScaleFactor, usedScaleFactor, 0, 0); // Replaces the old matrix completly
- // matrix.postTranslate((frameWidth - newImageWidth) / 2, frameHeight - newImageHeight);//BottomCrop
- matrix.postTranslate((frameWidth - newImageWidth) / 2, 0);//Top Crop
- setImageMatrix(matrix);
- return super.setFrame(frameLeft, frameTop, frameRight, frameBottom);
- }
- }
今天关于如何在 swift 中在自定义导航栏顶部制作一半的 ImageView和swiftui自定义导航栏的分享就到这里,希望大家有所收获,若想了解更多关于android 实现圆形 imageView,Circle imageView.、Android,ImageView over ImageView、Android:如何将 ImageView 的内容调整为与另一个 ImageView 内容相同的尺寸?、FitWidth ImageView 和 TopCrop ImageView等相关知识,可以在本站进行查询。
本文标签: