The man who ambushed a New York officer in a parked police vehicle earlier this week, gunning her down with a shot to the head, had been acting erratically in recent weeks and was “paranoid the police were following him,” an official said Thursday.

Alexander Bonds’s behavior had become so worrisome that his girlfriend called 911 in the hours before police say he shot and killed officer Miosotis Familia in the Bronx. But Bonds kept ducking out of sight, eventually reemerging to use a stolen gun, said New York Police Department Chief of Detectives Robert Boyce.

The grim account contributed to the emerging picture of Bonds, who had sprinkled his Facebook page with news stories and commentary about police misconduct.

For some, the portrait brought to mind similar ambushes that police say have been carried out by men who raged against police officers — including the gunman who killed five law enforcement officers in Dallas one year ago Friday.

“Make no mistake about it: Officer Familia is dead because of one reason and one reason only,” New York City Police Commissioner James P. O’Neill at a briefing Thursday. “And that’s Alexander Bonds and his hatred of police.”

The recent killings of law enforcement officers mark an uptick in line-of-duty deaths. Familia is among 24 officers fatally shot this year, up from 22 at the same point in 2016, according to the National Law Enforcement Memorial Fund, a nonprofit that tracks line-of-duty deaths. Four of those killings were ambush attacks, the group said.

Last year, there were 64 officers killed in shootings, up from 41 fatal shootings a year earlier and accounting for nearly half of all line-of-duty deaths nationwide.

Overall, law enforcement line-of-duty deaths have declined since the 1970s.

“These incidents are not new,” William J. Bratton, a former New York police commissioner, said in an interview Thursday. “They have always been a part of policing. … But when they do occur they attract an enormous amount of attention.”

Familia’s death reverberated far beyond the quiet Bronx community where she lived, sending a chill through law enforcement officials in the city and across the country.

On Thursday afternoon, politicians and public figures walked in and out of the brick apartment building where Familia lived with her twins, mother and a cat. Neighbors said they often saw the single mother at the playground across the street with her two youngest children. She also has a 20-year-old daughter, according to O’Neill.

Just after 11 a.m. Thursday, the 12-year-old twins emerged with several relatives and piled into a waiting NYPD van near the curb. A family friend explained they have business to attend to — the grim after work of a family death.

Patrick Lynch, president of the Patrolmen’s Benevolent Association, called Familia “a brave officer who just went out to do a day’s work in uniform, her job like so many others.”

“Unfortunately a vicious mad man took her life,” he said. “That’s a terrifying possibility every officer faces each day. But on days like this becomes painfully real.”

Familia was a 12-year veteran of the police department. Neighbors said they rarely saw her in her uniform, but Hector Payan, whose brother lives not far from Familia’s building, said Familia had talked about the meaning she found in her work of keeping people safe.

“She told me it was amazing work and I think around here, we appreciated it and I hope she knew that because that was a good lady,” he said. “She was really a part of this neighborhood.”

Familia was killed as she sat in a NYPD mobile command post just two miles from her home. Friends of Bonds said that he was a good person, but described him as troubled and shaken by his time in prison.

“He had to come back just really depressed about what happened to him,” said Karl Nemeth, a friend of Bonds who served with him during his sentencing at Attica Correctional Facility in Attica, N.Y.

Bonds served seven years for robbery and was released on parole in May 2013.

“You could tell it had broken him down,” Nemeth said of Bonds’s time behind bars.

Over the two weeks before the shooting, Bonds had become paranoid that police and emergency medical services personnel were following him, Boyce said. On July 1, Bonds’s girlfriend took him to a hospital for psychiatric treatment, but he was released not long after.

Boyce said police found several bottles of medications inside Bonds’s home, including antipsychotic drugs and antidepressants. A law enforcement official told The Post on Thursday that it did not appear Bonds was taking the medication at the time of the shooting.

In a video posted to Bonds’s Facebook page in September, he says, “I’m not playing, Mister Officer. I don’t care about a hundred police watching this s---. You see this face. You see this face or anything, leave it alone. Trust and believe.”

Some said the overall public mood toward police appeared to have improved recently, while others such as Bratton pointed to the gunman’s mental-health issues — and his access to a gun that police say was reported stolen in West Virginia in 2012.

“You put guns and mental health together and it’s a combustible mixture,” said Chuck Wexler, executive director of the Police Executive Research Forum, who regularly works with departments.

Yuseff Hamm, the president of the NYPD Guardians, a union of black NYPD officers, said many of his members are not harboring fear that they will be attacked, rather they are carrying anger that the relationship between police and some neighborhoods are so deeply deteriorated.

“As police officers, we face fear every day when we suit up and go protect the public,” Hamm said. “This doesn’t create additional fear. It’s anger. It’s like: you’re targeting someone because of the uniform that they’re wearing, not because they’ve done something to you.”

Emma Ockerman, Wesley Lowery and Julie Tate contributed to this report.